Par*_*war 1 sql postgresql null database-performance
很多时候我必须运行这个查询:
select * from users where name is not null and name != ''
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点。我需要更多的性能,任何建议。我想这是很常见的,所以可能会有一些编译的函数,就像
select * from users where name is present()
Run Code Online (Sandbox Code Playgroud)
使用 PostgreSQL 9 版本。
对于任何x,null != x将是null和null不是true。这意味着您可以简单地忽略您的案例中的 NULL 并说:
select * from users where name != ''
Run Code Online (Sandbox Code Playgroud)
如果您更清楚,您还可以使用 COALESCE 将 NULL 转换为空字符串:
select * from users where coalesce(name, '') != ''
Run Code Online (Sandbox Code Playgroud)
当然,那个coalesce电话不是免费的。
演示:http : //sqlfiddle.com/#!2/e810c/2
| 归档时间: |
|
| 查看次数: |
779 次 |
| 最近记录: |