本文介绍了T-SQL:通过连接选择要删除的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
场景:
假设我有两个表,TableA 和 TableB.TableB的主键是单列(BId),是TableA中的外键列.
Let's say I have two tables, TableA and TableB. TableB's primary key is a single column (BId), and is a foreign key column in TableA.
在我的情况下,我想删除 TableA 中与 TableB 中特定行链接的所有行:我可以通过联接来做到这一点吗?删除从连接中拉入的所有行?
In my situation, I want to remove all rows in TableA that are linked with specific rows in TableB: Can I do that through joins? Delete all rows that are pulled in from the joins?
DELETE FROM TableA
FROM
TableA a
INNER JOIN TableB b
ON b.BId = a.BId
AND [my filter condition]
或者我被迫这样做:
DELETE FROM TableA
WHERE
BId IN (SELECT BId FROM TableB WHERE [my filter condition])
我问的原因是在我看来,第一个选项在处理更大的表格时会更有效.
The reason I ask is it seems to me that the first option would be much more effecient when dealing with larger tables.
谢谢!
推荐答案
DELETE TableA
FROM TableA a
INNER JOIN TableB b
ON b.Bid = a.Bid
AND [my filter condition]
应该可以
这篇关于T-SQL:通过连接选择要删除的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!