当前位置: 代码迷 >> Sql Server >> 请教,能否查询某一字段成对的条目 ?多谢
  详细解决方案

请教,能否查询某一字段成对的条目 ?多谢

热度:51   发布时间:2016-04-24 10:21:39.0
请问,能否查询某一字段成对的条目 ?谢谢!
例如:
        AA        BB        CC
1       1           A          一
2       2           A          一
3       3           B          一
4       3           C          五
5       4           C          六
6       1           C         七

请问,以上表结构中,我想分别查询各字段中,成对出现的条目,比如:
对AA字段查询,可得到1,3,4,6条目 
对BB字段查询,可得到1,2条目
对CC字段查询,Recordcount=0
对AA字段查询(条件:BB<>'A'),可得到3,4条目 

请问,SQL语句怎么写啊?谢谢了,各位老大!
------解决方案--------------------
select aa,count(1) as num from tb group by aa having count(1)=2

------解决方案--------------------
--AA
select * from tb where exists 
  (select * from (select aa,COUNT(*) as num from tb group by aa having COUNT(*)=2) a where a.aa=tb.aa)
--BB
select * from tb where exists 
  (select * from (select bb,COUNT(*) as num from tb group by bb having COUNT(*)=2) a where a.bb=tb.bb)
--CC 没看懂
--AA
select * from tb where exists 
  (select * from (select aa,COUNT(*) as num from tb group by aa having COUNT(*)=2) a where a.aa=tb.aa)
    and bb<>'A'
  相关解决方案