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 的所有东西都返回了
![]() | 1 BrettD 2020-11-03 10:47:09 +08:00 via iPhone 为啥要这样写 |
![]() | 3 luxinfl OP 话说换成 in,比较容易看出来一下吧,应该是等价于 ``` select * from t1 where not in(select name from t2) 第二个好像不太对 ``` |
4 wisunny 2020-11-03 10:50:47 +08:00 via Android 翻译成汉语就好理解多了,一个是不存在相等的,一个是存在不等的。而存在不等的只要存在一个就始终为 true,结果自然不一样 |
![]() | 5 chendy 2020-11-03 10:51:49 +08:00 |
![]() | 9 kiracyan 2020-11-03 11:12:04 +08:00 select * from t1 where name not in (select name from t2) |
![]() | 10 no1xsyzy 2020-11-03 11:21:31 +08:00 ![]() |
![]() | 12 YYYYMMDDHHSS 2020-11-03 11:25:46 +08:00 |
![]() | 13 no1xsyzy 2020-11-03 11:27:39 +08:00 |
![]() | 14 no1xsyzy 2020-11-03 11:29:29 +08:00 |
![]() | 15 wolfie 2020-11-03 11:30:00 +08:00 t2 如果是空表,exists 直接返回 false 。 第二条 sql 跟你需求不一样。 |
![]() | 16 luxinfl OP |
![]() | 18 no1xsyzy 2020-11-03 12:53:18 +08:00 @luxinfl 啊,用 Python 解释的话这么写吧: 式 1: [x for x in t1 if not any(x['name'] == y['name'] for y in t2)] 根据反演律展开,其等价于 [x for x in t1 if all(x['name'] != y['name'] for y in t2)] 式 2: [x for x in t1 if any(x['name'] != y['name'] for y in t2)] 一个是 all 一个是 any |
20 samv2 2020-11-03 13:51:35 +08:00 |
![]() | 21 c6h6benzene 2020-11-03 14:03:52 +08:00 via iPhone @no1xsyzy #13 第二个就是标准的 sql 啊,一天到晚都写这种,name=name 是连接条件,where 是限制条件,不会读混的。另外要用 is null |
22 xdsty 2020-11-03 14:07:16 +08:00 直接左连接找悬空的记录就可以了 |
24 xiyangzuile 2020-11-03 14:56:35 +08:00 |