4

I have a MySQL table like this:

| id1 | id2 |

| 34567 | 75879 | <---- pair1

| 13245 | 46753 |

| 75879 | 34567 | <---- pair2

| 06898 | 00013 |

with 37 000 entries.

What is the SQL Request or how can i identify duplicates pairs (like pair1 and pair2)?

Thanks

Community
  • 1
  • 1

3 Answers3

3

if you want to identify the duplicates and count them at the same time, you could use:

SELECT if(id1 < id2, id1, id2), if (id1 < id2, id2, id1), count(*)
  FROM your_table
 GROUP BY 1,2
HAVING count(*) > 1

This does not perform a join, which might be faster in the end.

Dan Soap
  • 10,114
  • 1
  • 40
  • 49
  • It would be very interested to see the running time on your query against a JOIN, especially against a large table.+1 for the underdog query !!! – RolandoMySQLDBA Dec 26 '11 at 23:27
  • I say underdog because most would probably scoff a query not using a JOIN, but I like queries that come from thinking outside the box. – RolandoMySQLDBA Dec 26 '11 at 23:28
2

If you join the table with it self you can filter out the ones you need.

SELECT * 
  FROM your_table yt1,
       your_table yt2 
 WHERE (yt1.id1 = yt2.id2 AND yt1.id2 = yt1.id1)
    OR (yt1.id1 = yt2.id1 AND yt1.id2 = yt2.id2)
Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
0

The original post is 1000 years old, but here's another form:

SELECT CONCAT(d1, '/' d2) AS pair, count(*) AS total
FROM your_table
GROUP BY pair HAVING total > 1
ORDER BY total DESC;

May or may not perform as well as the other suggested answers.

rodrigo-silveira
  • 12,607
  • 11
  • 69
  • 123