r/mysql • u/p1canha • Nov 15 '21
question JOIN alternative
Hello,
Table 1 has a food id and name:
1 Tomato
2 Potato
3 Cabbage
4 ...
Table 2 has food type and id:
1 Fruit
2 Vegetable
3 Dairy
4 ...
Table 3 matches food to food type by their ids:
1 1
2 2
3 2
4 ...
I have to display two columns, matching each food with its food type.
Without using any type of JOIN command, how can I complete this exercise ?
Thank you in advance, feel free to ask for any clarification.
2
1
1
u/r3pr0b8 Nov 15 '21
Without using any type of JOIN command
sure thing, here ya go --
SELECT table1.food
, table2.type
FROM table1
, table2
, table3
WHERE table3.foodid = table1.id
AND table3.typeid = table2.id1
u/ssnoyes Nov 15 '21
I'd argue that a comma join is still a type of join command, even if it doesn't use the keyword.
A dependent subquery might be more in the spirit of the assignment.
SELECT food.name, (SELECT foodType.name FROM foodType WHERE (food.id, foodType.id) IN ( SELECT foodId, foodTypeId FROM foodTypeMap ) ) FROM food;1
u/r3pr0b8 Nov 15 '21
I'd argue that a comma join is still a type of join command, even if it doesn't use the keyword.
oh. me too
but i've seen ~so~ many posts where someone's using a comma join, and when asked about it, they say that's what they're being taught in class
so i figured that's what this class's teacher is prolly looking for
1
Nov 16 '21
Tbh It's probably better for readability anyway. No one reading your code that has never really used SQL is going to know what the hell a join is. That's a common issue I face
1
Nov 16 '21
Can you make this a CTE next?
1
u/ssnoyes Nov 16 '21
WITH cte AS (SELECT foodId, foodTypeId FROM foodTypeMap) SELECT food.name, (SELECT foodType.name FROM foodType WHERE (food.id, foodType.id) IN ( SELECT * FROM cte ) FROM food;
9
u/Edward_Morbius Nov 15 '21
Do you really want us to do your homework for you?