请教SQL查询重复记录的方法。例如有数据表有数据列N列假设为A1、A2、A3、A4、....An,有2万条记录,如果以A2列为判断标准,那么数据表中的大部分记录是唯一的,有一些以“A2”为判断标准的话,有2条或2条以上的记录重复记录。A2并不是主键,所以整个数据表是没有问题的。那么如何用SQL语句筛选出以“A2”为标准是重复的记录?

解决方案 »

  1.   

    select * from tb where a2 in (select a2 from tb group by a2 having(count(*)>1))
      

  2.   


    select A2 from 表
    group by A2
    having Count(A2)>1-->1 可以查出有1次以上重复记录的
    -->2 可以查出有2次以上重复记录的
      

  3.   

    简化能达到你的要求吗,不用怎么就说不行
    select * from 表A a where exists 
    (select 0 from 表A b where a.A2=b.A2 and a.A3=b.A3 and a.A4=b.A4 group by b.A2,b.A3,b.A4 having count(*)>1)
      

  4.   

    re:
    刚才的那个帖子写错了select tb.* from tb,(select a2,a3,a4 from tb group by a2,a3,a4 having count(*)>1) a
    where tb.a2=a.a2 and tb.a3=a.a3 and tb.a4 =a.a4你试试这个。这个问题:
    1楼的是正解
      

  5.   

    1楼正解
    select  c_no, pay_no  
    from table where c_no in (
    select a.c_no
    from table  a
    group by a.c_no
    having count(*) > 1 )
    and pay_no is not null
    group by c_no,pay_no
    order by 1