M
M
MdaUZH2016-01-22 15:01:33
PHP
MdaUZH, 2016-01-22 15:01:33

JOIN SQL from 4 tables?

Hello.
I started learning SQL in detail only recently.
And then he stumbled, how to pull information from tables (more than 2)?
For example, there is a table:
1. Recipe :
id | name | description | ...
2. Recipe components :
id | recept_id | parent_id | ...
recept_id -> recipe id from table 1, parent_id -> component id from table
3 3. Components
id | name | percent | ...
4. Flavors :
id | taste | recept_id
recept_id -> recipe id from table 1
Need to get:
1. All information from the recipe,
2. Information about the component (which is used in this recipe)
2. Flavors used in this recipe
Please tell me how it will look like, I'm not very friendly with JOIN yet, I only know how to pull it out of 2 tables and no more, I will be very grateful.
Thanks to all :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksej, 2016-01-22
@MdaUZH

I'm from the MS SQL world, so some inconsistencies are possible:

SELECT rcp.*, cmp.*, tst.*
FROM recept AS rcp
  INNER JOIN recept_component AS rcp_cmp ON rcp.id = rcp_cmp.recept_id
  INNER JOIN component AS cmp ON cmp.id = rcp_cmp.parent_id
  INNER JOIN taste AS tst ON tst.recept_id = rcp.id

instead of asterisks, indicate the specific fields that interest you
. ONLY recipes will be selected for which both flavors and ingredients exist.
I don't know if it's okay to post links here, but google "tsql join" from the pictures and you will find all join options with a visual explanation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question