0
votes

I want to pull out duplicate records in a MySQL database.

----------------------------
| 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     |
----------------------------

I would like to pull it so that it shows each row that is a duplicate. Something like :

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

or

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

Any thoughts on how this can be done?

4

4 Answers

2
votes
select group_concat(id) as duplicate_ids,
       columna, 
       columnb
from your_table
group by columna, columnb
having count(id) > 1
3
votes

try GROUP_CONCAT

SELECT    GROUP_CONCAT(id), column_A, column_B
FROM      mytable
GROUP BY  columnA, columnB
HAVING    COUNT(id) > 1
2
votes
SELECT
    GROUP_CONCAT(id) as ids,
    column_a,
    column_b 
FROM table
GROUP BY column_a, column_b 
HAVING COUNT(id) > 1
1
votes

try group_contact () function,

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