postgreSQL Query ISNULL



假设我有四个字段A、B、C、D只有当两个或多个字段不是空时,我才需要SQL查询来显示结果

对于Postgres,您可以使用:

select *
from the_table
where num_nonnulls(a,b,c,d) >= 2;

如果您想要与任何SQL数据库一起使用的ANSI SQL,则需要使用case表达式。它变得有点麻烦,因为这个案例很冗长,但相对简单:

select 
* 
from 
mytable
where 
case when a is null then 0 else 1 end 
+ case when b is null then 0 else 1 end 
+ case when c is null then 0 else 1 end 
+ case when d is null then 0 else 1 end 
>= 2;

最新更新