is [not] distinct from 的 null 比较
2015-11-03 10:19
411 查看
--在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 效率上不如<>,=所以尽量要少用
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 效率上不如<>,=所以尽量要少用
相关文章推荐
- PostgreSQL新手入门教程
- PostgreSQL教程(十):性能提升技巧
- PostgreSQL教程(二):模式Schema详解
- PostgreSQL教程(十三):数据库管理详解
- PostgreSQL教程(八):索引详解
- PostgreSQL教程(三):表的继承和分区表详解
- 简单介绍Ruby on Rails对PostgreSQL数组类型的支持
- PostgreSQL教程(十九):SQL语言函数
- PostgreSQL教程(四):数据类型详解
- PostgreSql新手必学入门命令小结
- 用一整天的时间安装postgreSQL NTFS权限
- FREEBSD安装POSTGRESQL笔记
- PostgreSQL8.3.3安装方法第1/2页
- 深入理解PostgreSQL的MVCC并发处理方式
- PostgreSQL教程(一):数据表详解
- PostgreSQL教程(十二):角色和权限管理介绍
- PostgreSQL教程(五):函数和操作符详解(1)
- 15个postgresql数据库实用命令分享
- Windows下Postgresql数据库的下载与配置方法