我有下表: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需要select查询才能返回:
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)发布于 2012-08-28 06:58:33
您可以在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请参阅SQL Fiddle with Demo
https://stackoverflow.com/questions/12150503
复制相似问题