表tt
序号     aa       bb         cc           dd
1      11111    3333       3333         345
2      22222    4444       6666         123
3      33333    4444       6666         231
4      44444    4444       3333         111
5      55555    4444       7777         222现在想把同时满足bb列、cc列中同时重复的数据提取出来
就是说我想要序号为:2、3的这两条数据,sql语句怎么写啊?

解决方案 »

  1.   


       select * from ttwhere bb in(select bb from tt group by bbhaving count(id)>1) and cc in(select cc from tt group by cc having count(id)>1)
      

  2.   

    效率高且简单的:select t1.id,t1.bb,t1.ccfrom tt t1 , tt t2where t1.id != t2.id and  t1.bb = t2.bb and t1.cc = t2.cc
      

  3.   

    select * from table where bb in (select bb from table group by bb having count(*)>1) and cc in (select cc from table group by cc having count(*)>1)我想到的是这样的  但是结果和预想的不一样
      

  4.   

    select tpp.* from 
    (select tx.c,tx.aa,tx.bb from (select COUNT(*) c,t.aa,t.bb from temp t
     group by t.aa,t.bb) tx where tx.c > 1) ttt 
     left join temp tpp on ttt.aa=tpp.aa and ttt.bb=tpp.bb
      

  5.   

    select a.* from tt a
    inner join
      (select bb,cc from tt group by bb,cc having count(*)>1) b
      on a.bb=b.bb and a.cc=b.cc这个语句可以啊,谢谢这位朋友
      

  6.   

    select a.* from a where exists (select 'x' from a a2 where a2.id!=a.id and a2.cc=a.cc and a2.bb=a.bb)