Jamie
Jamie

Reputation: 371

MySQL select re-ask+simplification

The original question is here.. MySQL self-referencing ID and selects

I would like to pose the question in a way with all the relation to a specific case removed.

I have the example table..

id1     id2
1        5
5        1
2        3
3        2

What SQL command would return..

id1     id2
1       5
2       3

Essentially removing the "duplicate rows".

Upvotes: 1

Views: 63

Answers (1)

kylex
kylex

Reputation: 14406

Q1 and Q2 are the alias' I've created for your table, so we can reference the id's as if they were on different tables.

DELETE Q1 FROM table Q1 
     JOIN table Q2 
     ON Q1.id1 = Q2.id2 
     AND Q2.id1 = Q1.id2
     WHERE Q1.id1 > Q1.id2

Upvotes: 2

Related Questions