I’m somewhat new to MySQL and I’m not sure how to do this or if it’s even possible.
I have a table like this:
Table products
id | model | alt_model
-- ----- ---------
1 abc abc
2 ab-c abc
3 a-bc abc
4 def def
5 ghi ghi
6 gh-i ghi
7 jk-l jkl
8 mno mno
9 m-n-o mno
10 mn-o mno
11 m-no mno
12 pqr pqr
13 stu stu
I want to be able to do a query that will result in every duplicate combination of products where the alt_models are the same. Each pair of duplicates is unique, switching their order should not yield another result, but the result should have the lower id in the id1 field
The result should look like this:
id1 | id2 | model1 | model2
--- --- ------ ------
1 2 abc ab-c
1 3 abc a-bc
2 3 ab-c a-bc
5 6 ghi gh-i
8 9 mno m-n-o
8 10 mno mn-o
8 11 mno m-no
9 10 m-n-o mn-o
9 11 m-n-o m-no
10 11 mn-o m-no
Is this possible?
A simple self join should do it;
SQLfiddle for testing here.