sql - 排除 WHERE 子句中的空白值和空值

标签 sql postgresql null database-performance

很多时候我必须运行这个查询:

select * from users where name is not null and name != ''

有没有更好的方法来做到这一点。我需要更多的性能,任何建议。我想这很常见,所以可能会有一些编译函数类似于

select * from users where name is present()

使用 PostgreSQL 9 版本。

最佳答案

对于任何 xnull != x 将是 nullnull 不是 true 。这意味着您可以简单地忽略您的案例中的 NULL 并说:

select * from users where name != ''

如果您更清楚的话,您还可以使用 COALESCE 将 NULL 转换为空字符串:

select * from users where coalesce(name, '') != ''

当然,coalesce 调用不是免费的。

演示:http://sqlfiddle.com/#!2/e810c/2

关于sql - 排除 WHERE 子句中的空白值和空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21948011/

相关文章:

postgresql - 使用 LIMIT 在 JOINED 表中获取 NULL 值

python - 如何通过按键组合多个查询集?

ruby - 对多个变量检查​​ nil

mysql - SQL,难以获取数据查询

sql - 忽略负号 SQL SERVER

MySQL COUNT 和 GROUP BY 子查询

sql - 相互组合查询

postgresql - 如何在 ASP.NET Boilerplate Core 2.0 模板中使用 Npgsql?

android - ConnectivityManager getActiveNetworkInfo() 始终为空,即使数据流量处于 Activity 状态也是如此

c++ - 无效的空指针 - C++ - VS2010