首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择不同的%2列

选择不同的%2列
EN

Stack Overflow用户
提问于 2012-08-28 06:30:26
回答 1查看 164关注 0票数 0

我有下表:ID1 , ID2, Name, Sex

表中有重复ID1但ID2、名称和性别不同的记录--还有重复ID2和不同ID1、名称和性别的记录。ID1和ID2都可以有空值,但不能用于同一条目。我需要为id1和id2选择非重复记录,例如

代码语言:javascript
复制
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查询才能返回:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-28 06:58:33

您可以在WHERE子句中使用EXISTS

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12150503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档