小问题,大苦恼(not in 与 not exists)

yanleigis yanleigis 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
两个表toparcel,tcgrade,内容如下
SQL> select GradeLevel from tcgrade;

GRAD
----
1
2
3
4
5
SQL> select gradelevel,count(*) from toparcel
2 where substr(LandUsecode,1,2)='11' group by gradelevel;

GRAD COUNT(*)
---- ----------
1 166
2 126
3 103
4 231
5 211
14

使用not in
SQL> select count(*) from toparcel a
2 where substr(LandUsecode,1,2)='11' and
3 (a.gradelevel not in (select GradeLevel from tcgrade));

COUNT(*)
----------
0
显然不对
SQL> select count(*) from toparcel a
2 where substr(LandUsecode,1,2)='11' and
3 not (a.gradelevel in (select GradeLevel from tcgrade));

COUNT(*)
----------
0
也不对
而这样是对
SQL> select count(*) from toparcel a
2 where substr(LandUsecode,1,2)='11' and not exists
3 (select GradeLevel from tcgrade b where b.gradelevel=a.gradelevel);

COUNT(*)
----------
14
我想知道,not in怎么写是正确的


...全文
评论 5
收藏 已收藏
分享
返回顶部
yanleigis
基础和管理 17,377 成员 95,111 内容