首页 > 数据库 >postgresql/lightdb的 is distinct from、is not distinct from

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

时间:2023-01-05 11:15:10浏览次数:65  
标签:postgresql lightdb cast distinct column null select postgres

在 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)

 






标签:postgresql,lightdb,cast,distinct,column,null,select,postgres
From: https://www.cnblogs.com/lightdb/p/17026944.html

相关文章

  • postgresql/lightdb OVERLAPS 和 BETWEEN SYMMETRIC函数介绍
    PostgreSql/lightdb中有两个非常方便、有用的比较操作,OVERLAPS和BETWEENSYMMETRIC。1.OVERLAPS(重叠)OVERLAPS操作,用于检测两个日期范围是否重叠。举例:SELECT(......
  • postgresql/lightdb中覆盖自增列值
    PostgreSQL里的自动生成标识列identitycolumn和自动生成存储列generatedcolumn是非常不错的功能,不过他们的实现语法比较近似,容易弄混,本文将进行示例介绍。PGv10:......
  • PostgreSQL远端访问
    PostgreSQL默认的理念是运行在本地地址且不允许外部访问的。如果想通过NavicatforpostgreSql这种优秀的第三方软件访问需要做出如下修改:一、启动在外部可访问的地址上......
  • postgresql_anonymizer使用
     瀚高数据库目录环境文档用途详细信息  环境系统平台:Linuxx86-64RedHatEnterpriseLinux7版本:12 文档用途postgresql_anonymizer是对数据库中的个人识别信息或商业......
  • Postgresql分析慢sql
    Postgresql分析慢sqli查拉图斯特拉如是说2022年12月20日21:06 ·  阅读446开启掘金成长之旅!这是我参与「掘金日新计划·12月更文挑战」的第1天,点击查看......
  • PostgreSQL源码结构
    PostgreSQL源码结构  5440 次浏览      6 2019-9-6 编辑推荐:本文来自于csdn,本文主要介绍了PostgreSQL......
  • PostgreSQL死锁了怎么办?
    PostgreSQL死锁了怎么办?慕枫技术笔记2022年05月27日00:17 ·  阅读638持续创作,加速成长!这是我参与「掘金日新计划·6月更文挑战」的第1天,点击查看活动详......
  • Oracle转PostgreSQL
    Oracle转PostgreSQLOracle postgresql oracle sql 数据库 最近在做一些OracleSQL转PostgreSQL的工作,顺便记录这些改变,以便以后再转换有个参考。描述OracleP......
  • PostgreSQL动态SQL(兼容oracle DBMS_SQL)
    PostgreSQL动态SQL(兼容oracleDBMS_SQL)PostgreSQL sql 数据库 postgresql oracle中的dbms_sql包可以用来执行动态SQL,让我们在存储过程的动态SQL中使用prepared......
  • postgresql windows 开发环境搭建
    postgresqlwindows开发环境搭建发布时间:2020-06-2912:45:05 来源:网络 阅读:2883 作者:pgmia 栏目:数据库一、软件需求Windows7旗舰版sp1X64ActivePerl-5.......