改进 mysql 查询
我有一个疑问。
DELETE FROM A
WHERE i NOT IN
( SELECT i FROM B WHERE j = 1
UNION select i from C
UNION select i from D
);
基本上删除 A 中字段 i
未出现在表 B、C 或 D 中的所有行。如果只是:
DELETE FROM A
WHERE i NOT IN
( SELECT i FROM B
);
那么可以使用左连接轻松完成
DELETE A FROM A
LEFT JOIN B
ON A.i = B.i
WHERE B.id is NULL;
(假设每个表都有一个 id模式中的字段)
我想我的问题是上面的内容是否扩展到 三表场景有以下解决方案吗?
DELETE A FROM A
LEFT JOIN B
ON A.i = B.i AND B.j = 1
LEFT JOIN C
ON A.i = C.i
LEFT JOIN D
ON A.i = D.i
WHERE B.id is NULL
AND C.id is NULL
AND D.id is NULL
I have a query.
DELETE FROM A
WHERE i NOT IN
( SELECT i FROM B WHERE j = 1
UNION select i from C
UNION select i from D
);
Basically delete all rows in A where field i
does not occur in tables B, C or D. If it was just:
DELETE FROM A
WHERE i NOT IN
( SELECT i FROM B
);
Then that could be done easily with a left join
DELETE A FROM A
LEFT JOIN B
ON A.i = B.i
WHERE B.id is NULL;
( Assume that every table has a id field in the schema )
I guess my question is then does the above extend to
the three table scenario with the following solution?
DELETE A FROM A
LEFT JOIN B
ON A.i = B.i AND B.j = 1
LEFT JOIN C
ON A.i = C.i
LEFT JOIN D
ON A.i = D.i
WHERE B.id is NULL
AND C.id is NULL
AND D.id is NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的事情:
Something like this: