如何使用MySQL列出所有重复值?

时间:2012-08-08 01:52:53

标签: mysql

我有一个包含以下字段的表:

 id | domainname | domain_certificate_no | keyvalue

select语句输出的示例可以是:

 id    |  domainname               |  domain_certificate_no      |  keyvalue
 ===============================================================================
'57092', '02a1fae.netsolstores.com', '02a1fae.netsolstores.com_1', '55525772666'
'57093', '02a1fae.netsolstores.com', '02a1fae.netsolstores.com_2', '22225554186'
'57094', '02a1fae.netsolstores.com', '02a1fae.netsolstores.com_3', '22444356259'
'97168', '02aa6aa.netsolstores.com', '02aa6aa.netsolstores.com_1', '55525772666'
'97169', '02aa6aa.netsolstores.com', '02aa6aa.netsolstores.com_2', '22225554186'
'97170', '02aa6aa.netsolstores.com', '02aa6aa.netsolstores.com_3', '22444356259’

字段domain_certificate_no是唯一的。我使用group by来查找重复的keyvalue但我实际需要的不仅仅是keyvalue的出现次数,而且还需要逐个列出重复值每个记录中的其他字段发生此值。 group by无法实现的目标。我怎么能这样做。

3 个答案:

答案 0 :(得分:1)

没有经过测试,但我认为这样可以解决问题

SELECT * FROM table 
    WHERE keyvalue IN (
      SELECT keyvalue FROM table GROUP BY keyvalue HAVING COUNT(*) > 1
    )

答案 1 :(得分:1)

来自其他人答案的替代方案是来自派生查询的INNER JOIN

SELECT  a.*
FROM    myTable a
            INNER JOIN
            (
                SELECT keyValue
                FROM myTable
                GROUP BY keyValue
                HAVING COUNT(keyValue) > 1
            ) b 
                ON a.keyValue = b.keyValue

答案 2 :(得分:0)

没有测试代码,但它可以帮助你更多;)

select group_concat(id) as all_id, group_concat(domainname) as all_domains, group_concat(domain_certificate_no) as all_certificats , keyvalue, count(keyvalue) from table x
group by keyvalue
order by count(keyvalue)