Which version of the request is faster / better? (Curiosity.)
More importantly, are they equivalent?
Do these queries do the same in this example?
1) INNER JOIN with two OR conditions:
SELECT DISTINCT (cat.id) as 'Cat ID:' FROM cat INNER JOIN cuteness_showdown ON (cat.id = cuteness_showdown.cat_1 OR cat.id = cuteness_showdown.cat_2);
2) Request each column separately and UNION ALL:
SELECT DISTINCT (table_1.id) as 'Cat ID:' FROM (SELECT cuteness_showdown.cat_1 AS id FROM cuteness_showdown UNION ALL SELECT cuteness_showdown.cat_2 AS id FROM cuteness_showdown) AS table_1;
Now, which version is faster / better if I need a column from another table?
1) INNER JOIN with two OR conditions (no change):
SELECT DISTINCT (cat.id) as 'Cat ID:', cat.name as 'Cat Name:' FROM cat INNER JOIN cuteness_showdown ON (cat.id = cuteness_showdown.cat_1 OR cat.id = cuteness_showdown.cat_2);
2) Request each column separately and UNION ALL (necessary for the INNER JOIN cat table):
SELECT DISTINCT (table_1.id) as 'Cat ID:' cat.name as 'Cat Name:' FROM (SELECT cuteness_showdown.cat_1 AS id FROM cuteness_showdown UNION ALL SELECT cuteness_showdown.cat_2 AS id FROM cuteness_showdown) AS table_1 INNER JOIN cat on (table_1.id = cat.id);
Johnb source share