今天写了一个sql:
SELECT * from tableA a where a.mobile not in (select b.mobile from tableB b);
在本地的一个测试数据库执行能查出数据,然后放外网查询就没有数据。
经过确定,外网是应该查出数据的,不知道为什么,然后项目经理给了我另一条sql
select a.mobile,b.mobile from tableA a left join tableB b on a.id=b.id
对比了一下,分析了一下,测试数据库能查出数据,是因为mobile没有为null的数据。
带着疑问网上搜了下,发现not in (子查询) 的问题,如果 子查询有一个为null,都会导致结果为0,所以才会查不出数据。
最后解决为
SELECT * from tableA a where a.mobile not in (select b.mobile from tableB b where b.mobile is not null);
这样就子查询就不会查出为null的数据,也就没有问题了。
网上说in会替换为 or aa=aa or bb=bb .. 而not in会替换为 and a<>a and b<>b 这个我还没怎么理解
才发现,自己对sql查询语句还是很薄弱,需要提高了。