簡體   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