很多时候我必须运行这个查询:
select * from users where name is not null and name != ''
有没有更好的方法来做到这一点。我需要更多的性能,任何建议。我想这很常见,所以可能会有一些编译函数类似于
select * from users where name is present()
使用 PostgreSQL 9 版本。
最佳答案
对于任何 x
,null != x
将是 null
而 null
不是 true
。这意味着您可以简单地忽略您的案例中的 NULL 并说:
select * from users where name != ''
如果您更清楚的话,您还可以使用 COALESCE 将 NULL 转换为空字符串:
select * from users where coalesce(name, '') != ''
当然,coalesce
调用不是免费的。
关于sql - 排除 WHERE 子句中的空白值和空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21948011/