postgresql/lightdb的 is distinct from、is not distinct from

在 postgresql/lightdb 开发过程中有时会用到 is distinct from 和 is not distinct from 这个功能。

is distinct from

功能描述

A和B的数据类型、值不完全相同返回 true
A和B的数据类型、值完全相同返回 false
将空值视为相同。

复制代码
postgres=# \x
Expanded display is on.
postgres=# select 1 is distinct from 1,
1 is distinct from 2,
1 is distinct from '1',
'1' is distinct from '1',
1 is distinct from null,
null is distinct from null
;
-[ RECORD 1 ]
?column? | f
?column? | t
?column? | f
?column? | f
?column? | t
?column? | f
复制代码

is not distinct from

功能描述

A和B的数据类型、值不完全相同返回 false
A和B的数据类型、值完全相同返回 true
将空值视为相同。

复制代码
postgres=# \x
Expanded display is on.
postgres=# select 1 is not distinct from 1,
1 is not distinct from 2,
1 is not distinct from '1',
'1' is not distinct from '1',
1 is not distinct from null,
null is not distinct from null
;
-[ RECORD 1 ]
?column? | t
?column? | f
?column? | t
?column? | t
?column? | f
?column? | t
复制代码

第三条看起来有点不太符合规则

postgres=# select 1 is not distinct from '1';
-[ RECORD 1 ]
?column? | t

为 t ,这怎么理解了?

postgres=# select pg_typeof(1),pg_typeof('1');
-[ RECORD 1 ]------
pg_typeof | integer
pg_typeof | unknown

有意思吧,pg_typeof(‘1’) 居然是 unknown, 而我们把它想象成字符串了。

复制代码
postgres=# select 1 is not distinct from cast('1' as varchar);
ERROR:  operator does not exist: integer = character varying
LINE 1: select 1 is not distinct from cast('1' as varchar);
                 ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

postgres=# select 1 is not distinct from cast('1' as int);
 ?column? 
----------
 t
(1 row)
复制代码
复制代码
postgres=# select cast('1' as varchar) is not distinct from 1;
ERROR:  operator does not exist: character varying = integer
LINE 1: select cast('1' as varchar) is not distinct from 1;
                                    ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

postgres=# select cast('1' as varchar) is not distinct from cast('1' as varchar);
 ?column? 
----------
 t
(1 row)
复制代码

 




posted @   zhjh256  阅读(96)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2021-01-05 [ERROR] code: 505, UNEXPECTED_FRAME - expected content header for class 60, got non content header frame instead, recoverable: false, server: true. Error: RabbitMQ Connection Closed
2017-01-05 glibc运行时库/gcc/gdb各版本发布时间以及rhel/centos默认glibc版本、lsb对应关系
2017-01-05 mysql主从之slave-skip-errors和sql_slave_skip_counter
点击右上角即可分享
微信分享提示