sql - SQL:“条件不为真”模式替代“为空或不是(条件)”
问题描述
在 SQL(特别是 Postgres)中:
where not foo='bar'
case is null的子句foo
评估为某种null
,导致该行不包含在结果中。
另一方面,where (foo='bar') is not true
where foo
is null 子句的计算结果为true
(该行包含在结果中)。
那么使用(foo='bar') is not true
等价于foo is null or not foo='bar'
吗?
似乎人们出于某种原因没有使用这种模式,我错过了什么吗?
解决方案
那么使用
(foo='bar') is not true
等价于foo is null or not foo='bar'
吗?
是的。或更简单:
foo IS NULL OR foo <> 'bar'
或者更简单,但是:
foo IS DISTINCT FROM 'bar'
使用后者。
手册章节比较函数和运算符中的详细信息。
有关的:
推荐阅读
- neo4j - 加快密码查询
- python-3.x - 如何将输出转换为列表并存储?
- php - 如何从地址php获取纬度和经度
- gnuplot - gnuplot 中的自动偏移
- android - androidx 找不到符号 androidx.databinding.DataBindingComponent
- macos - 无法列出我的 Developer ID 应用程序身份
- sql - SQL 查询在高负载期间返回不正确的结果
- postgresql - 为什么 gorm postgresql 在“)”处或附近抛出 pq:语法错误?
- java - 为什么转储中有这么多 SessionFactoryImpl 对象?
- elisp - 需要扩展elisp函数