is [not] distinct from 的 null 比较



--在postgresql中经常会比较两个值相同或者不同,但如果比较表达中有一个为null则会比较头痛,如下:


postgres=# select null <> null ,null = null ,null is null,null is not null;
 ?column? | ?column? | ?column? | ?column? 
----------+----------+----------+----------
          |          | t        | f
 
--创建测试数据
postgres=# create table t (id int,name varchar(20));
CREATE TABLE
 
postgres=# insert into t values(1,'rudy');
INSERT 0 1
postgres=# insert into t values(2);       
INSERT 0 1
postgres=# select * from t;
 id | name 
----+------
  1 | rudy
  2 | 
(2 rows)


--expression如果为null与任何值比较,都为null
postgres=# select * from t where null <> name;
 id | name 
----+------
(0 rows)


--is distinct from 对与非null其意为<>
postgres=# select * from t where 'rudy' is distinct from name; 
 id | name 
----+------
  2 | 
--is distinct from 对于expression为null,其会让与null的对比为值,非null对比其本身为假,is distinct from本身比较其是否不同
postgres=# select * from t where null is distinct from name;
 id | name 
----+------
  1 | rudy
(1 row)
--is not distinct from 对与非null其意为=
postgres=# select * from t where 'rudy' is not distinct from name;
 id | name 
----+------
  1 | rudy
--is distinct from 对于expression为null,其会让与null的对比为值,非null对比其本身为假,is distinct from本身比较其是相同  
postgres=# select * from t where null is not distinct from name;
 id | name 
----+------
  2 | 
  
--注意  is [not] distinct from 效率上不如<>,=所以尽量要少用
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值