例如:
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'