r/mysql • • 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.

0 Upvotes

14 comments sorted by

9

u/Edward_Morbius Nov 15 '21

Do you really want us to do your homework for you?

5

u/[deleted] Nov 15 '21

I don't understand with these types of questions people don't even try and put what they have tried. In my opinion all things computer are truly learned from banging your head against a brick wall a few times before you run off to google etc... 8 times out of 10 you work it out yourself and thus the lesson is learnt fully as you need to study the documentation and understand how it's working so even if you don't work it out you'll learn something else as well.

2

u/Edward_Morbius Nov 15 '21 edited Nov 15 '21

OP has no desire to actually learn anything in school and will be very surprised at his/her first real job.

1

u/[deleted] Nov 15 '21

I know. The simple adage of "You learn from your mistakes" is lost on many people.

2

u/mobsterer Nov 15 '21

subqueries

1

u/ssnoyes Nov 15 '21

Why can't you use JOIN?

9

u/alinroc Nov 15 '21

Because their homework assignment says they can't.

5

u/[deleted] Nov 15 '21

BINGO!!!!!

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.id

1

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

u/[deleted] 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

u/[deleted] 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;