繁体   English   中英

SQL在多列中找到至少一个不为空

[英]SQL to find atleast one not nulls in multiple columns

我的桌子上有一些非关键属性。 我想知道如何编写查询以查找键列,在这些键列中任何一行我都找不到空值。

例如1:

Key1 Key2 NonKey1 NonKey2    
k1   k2   nk1      nk2    
k1   k2   null     nk2    
k1   k2   nk1      null  

例如2:

Key1 Key2 NonKey1 NonKey2 NonKey3    
k1   k2   null     nk2     nk3
k1   k2   nk1      nk2     null

预期:

Key1 Key2 NonKey1 NonKey2
k1   k2   nk1      nk2

您是否只想为每个非键列获取一个值?

select key1, key2, max(nonkey1), max(nonkey2)
from mytable
group by key1, key2;

像这样:

    select key1, key2, max(nonkey2), max(nonkey3)
    from MyTable
    having max(nonkey2) is not null or max(nonkey3) is not null
    group by key1, key2

(这是SQL Server,但您明白了。)

其他方法:

select distinct key1, key2
from MyTable
where nonkey2 is not null and nonkey3 is not null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM