一、tb1与tb2都有Null值:
SELECT * FROM tb1 EXCEPT SELECT * FROM tb2;
SELECT * FROM tb1 WHERE id NOT IN(SELECT id FROM tb2);--得不到任何值
二、只有tb2有Null值:
SELECT * FROM tb1 EXCEPT SELECT * FROM tb2;
SELECT * FROM tb1 WHERE id NOT IN(SELECT id FROM tb2);--得不到任何值
结果与“一”相同
三、tb1与tb2都没有Null值:
SELECT * FROM tb1 EXCEPT SELECT * FROM tb2;
SELECT * FROM tb1 WHERE id NOT IN(SELECT id FROM tb2);
四、只有tb2有Null值:
SELECT * FROM tb1 EXCEPT SELECT * FROM tb2;
SELECT * FROM tb1 WHERE id NOT IN(SELECT id FROM tb2);
以上例子说明:
except会去重复, not in 不会(除非你在select中显式指定)
except用于比较的列是所有列, 除非写子查询限制列, not in 没有这种情况
表tb2中如果有null值的话,not in查询得不到值(如:A、B)
表tb1中如果有null值,not in不会查询出这个null值(如:D),而except可以查询到
当然通过对子查询指定不为NULL的话,NOT IN自然会得到值,如:
SELECT * FROM tb1 WHERE id NOT IN(SELECT id FROM tb2 WHERE ID IS NOT NULL);
来源:http://zxl08.blog.163.com/blog/static/24391322011101412232747/