[英]Select Distinct 2 Columns
我有下表: ID1 , ID2, Name, Sex
在表中有ID1重復但ID2,名稱和性別不同的記錄 - 還有ID2重復且ID1,名稱和性別不同的記錄。 ID1和ID2都可以具有空值,但不能用於相同的條目。 我需要為id1和id2選擇非重復記錄,例如
id1 id2 name sex
10 null jack M
10 null tom M
null 40 jennie F
null 32 jenie F
null 32 emma M
10 null stevie M
需要選擇查詢才能返回:
id1 id2 name sex
10 any any any (any means it can be either jack,tom,stevie)
null 40 jennie F
null 32 any any2 (any2 meaning jeniw or emma)
您可以在WHERE
子句中使用EXISTS
:
select t1.id1,
t1.id2,
name,
sex
from yourtable t1
where exists (select *
from yourtable t2
where t1.id1 = t2.id1
or t1.id2 = t2.id2)
group by t1.id1, t1.id2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.