如下sql语句
select aid from A where bid not in(select bid from B where bname='')
如果bname的记录在数据库中没有 就相当于 in(null) 此时 not in(null) 按理是要查询所有的A表中的记录
事实确实一条都没查出来
可以用以下语句测试以下
select aid from A where bid not in(null)
其实就是因为 相当于 bid!=null 这样是没有记录查询出来的
如果用 bid is null就能查询出来
可以修改sql语句未
select aid from A where bid not in(select nvl(bid,'-1') from B where bname='')
这样不会出现空的结果 将-1当作空来使用 前提是bid中不能出现 -1的结果 不然就不行了
-1的值视情况而定
本文探讨了一个SQL查询中的常见问题:当使用NOT IN操作符与NULL值进行比较时,可能会导致意外的查询结果。通过示例分析,提出使用NVL函数等方法来避免NULL值引发的问题,确保查询的准确性和效率。
1154

被折叠的 条评论
为什么被折叠?



