话说这两个 sql 为啥和我预想的不太一样

2020-11-03 10:36:20 +08:00
 luxinfl
select * from t1  where  not exists (select 1 from t2 where t1.name = t2.name)

select * from t1  where exists (select 1 from t2 where t1.name != t2.name)

我本意是找 t1 比 t2 多的数据,结果把 t1 的所有东西都返回了

2859 次点击
所在节点    程序员
24 条回复
c6h6benzene
2020-11-03 14:03:52 +08:00
@no1xsyzy #13 第二个就是标准的 sql 啊,一天到晚都写这种,name=name 是连接条件,where 是限制条件,不会读混的。另外要用 is null
xdsty
2020-11-03 14:07:16 +08:00
直接左连接找悬空的记录就可以了
luxinfl
2020-11-03 14:10:23 +08:00
@samv2 这个是可以,我只是没搞太懂 exists 和 not exists 怎么玩的
xiyangzuile
2020-11-03 14:56:35 +08:00
第二条是因为 内部查询判断 t1.name !=t2.name 会导致永远都返回 true , 所以返回结果是 t1 的所有结果

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

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

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

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

© 2021 V2EX