场景:
假设我有两个表,TableA和TableB.TableB的主键是单列(BId),是TableA中的外键列.
在我的情况下,我想删除TableA中与TableB中的特定行链接的所有行:我可以通过连接执行此操作吗?删除从连接中提取的所有行?
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])
我问的原因是在我看来,在处理较大的表时,第一个选项会更有效.
谢谢!
DELETE TableA FROM TableA a INNER JOIN TableB b ON b.Bid = a.Bid AND [my filter condition]
应该管用
我会使用这种语法
Delete a from TableA a Inner Join TableB b on a.BId = b.BId WHERE [filter condition]
是的你可以.示例:
DELETE TableA FROM TableA AS a INNER JOIN TableB AS b ON a.BId = b.BId WHERE [filter condition]
尝试使用访问数据库执行此操作,发现我需要在删除后立即使用.*.
DELETE a.* FROM TableA AS a INNER JOIN TableB AS b ON a.BId = b.BId WHERE [filter condition]
它在MySQL中几乎相同,但您必须在"DELETE"后面使用表别名:
DELETE a FROM TableA AS a INNER JOIN TableB AS b ON a.BId = b.BId WHERE [filter condition]