【问题标题】:Find ids with duplicate records in MySQL在 MySQL 中查找具有重复记录的 id
【发布时间】:2012-08-04 05:20:39
【问题描述】:

我想提取 MySQL 数据库中的重复记录。

----------------------------
| id | column_a | column_b |
|----|----------|----------| 
| 1  |    a     |    b     |
| 2  |    a     |    c     |
| 3  |    a     |    d     |
| 4  |    a     |    b     |
| 5  |    b     |    e     |
| 6  |    c     |    a     |
| 7  |    c     |    d     |
| 8  |    e     |    a     |
| 9  |    e     |    a     |
----------------------------

我想将其拉出,以便显示重复的每一行。类似的东西:

------
| id |
|----|
| 1  |
| 4  |
| 8  |
| 9  |
------

---------------------------------------
| duplicate_ids | column_a | column_b |
|---------------|----------|----------| 
|      1,4      |    a     |    b     |
|      8,9      |    e     |    a     |
---------------------------------------

对如何做到这一点有任何想法吗?

【问题讨论】:

    标签: mysql


    【解决方案1】:

    试试GROUP_CONCAT

    SELECT    GROUP_CONCAT(id), column_A, column_B
    FROM      mytable
    GROUP BY  columnA, columnB
    HAVING    COUNT(id) > 1
    

    【讨论】:

      【解决方案2】:
      SELECT
          GROUP_CONCAT(id) as ids,
          column_a,
          column_b 
      FROM table
      GROUP BY column_a, column_b 
      HAVING COUNT(id) > 1
      

      【讨论】:

        【解决方案3】:

        试试group_contact ()函数,

        select group_concat(id) duplicate_ids ,column_a, column_b
        from <table>
        group by column_a, column_b
        

        【讨论】:

          【解决方案4】:
          select group_concat(id) as duplicate_ids,
                 columna, 
                 columnb
          from your_table
          group by columna, columnb
          having count(id) > 1
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-10-25
            • 1970-01-01
            • 2014-03-18
            • 2021-12-21
            • 2019-08-08
            • 2023-03-11
            相关资源
            最近更新 更多