标签:
一、Null不支持大小/相等判断
1、下面的2个查询,不管表 users 中有多少条记录,返回的记录都是0行
select * from users where deleted_at = null;
select * from users where deleted_at != null;
用常规的比较操作符(normal conditional operators)来将 null 与其他值比较是没有意义的。 Null 也不等于 Null
2、将某个值与 null 进行比较的正确方法是使用 is 关键字, 以及 is not 操作符:
select * from users where deleted_at is null;
二、not in 与 Null
1、not in 实例
子查询(subselect)是一种很方便的过滤数据的方法。例如,如果想要查询没有任何包的用户,可以编写下面这样一个查询:
select * from users where id not in (select user_id from packages)
2、假若 packages 表中某一行的 user_id 是 null 的话,返回结果是空的!
3、出现上述的原因
例如
select * from users
where id not in (1, 2, null)
这个SQL语句会等效于
select * from users where id != 1 and id != 2 and id != null
4、in查询
select * from users
where id in (1, 2, null)
等效于
select * from users
where id = 1 or id = 2 or id = null
三、GROUP BY会把NULL分到一个组
参考资料:SQL 中 Null 值使用时需要注意的地方 http://www.studyofnet.com/news/1037.html
标签:
原文地址:http://my.oschina.net/u/2428791/blog/524908