关于 sql 查询和删除....请教!

2018-07-20 19:13:10 +08:00
 cnmllll

select A.ID from A left join B on A.ID=B.ID where B.ID is null

1.我用以上语句搜出了和 B 表未关联的 A.ID

2.请问我该如何写 sql 才能直接删除这些 A.ID ,既一条语句删除查出来未关联的 ID

3057 次点击
所在节点    MySQL
8 条回复
cnmllll
2018-07-20 19:20:47 +08:00
delete from A.ID where A.ID not not in (select A.ID from A left join B on A.ID=B.ID where B.ID is null)
这样可以么
cnmllll
2018-07-20 19:21:13 +08:00
delete from A where A.ID not not in (select A.ID from A left join B on A.ID=B.ID where B.ID is null)
这个
jintianfengda
2018-07-20 19:21:31 +08:00
DELETE
a
FROM
a
LEFT JOIN
b
ON
a.ID = b.ID
WHERE
b.id is null
cnmllll
2018-07-20 19:35:51 +08:00
delete from A where A.ID in (select A.ID from A left join B on A.ID=B.ID where B.ID is null) 这个终极版
akira
2018-07-20 19:39:52 +08:00
这个 sql 和 select id from A where id is null 有啥区别
lrh3321
2018-07-20 19:48:39 +08:00
@akira 表 A 中的行在表 B 中不一定有对应的行,他要删的就是这些无用的行。
akira
2018-07-20 19:51:38 +08:00
@lrh3321 嗯呐,审题不仔细 >"<
iwiki
2018-07-20 22:43:44 +08:00
delete from a where not exists (select 1 from b where a.id=b.id)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/472755

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX