gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:11:28 28 4
gpt4 key购买 nike

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

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com