0
0
0ldn0mad2019-04-18 19:49:02
SQL
0ldn0mad, 2019-04-18 19:49:02

How to make a selection from three tables in SQL?

I can not understand how to make a request to select three fields from three different tables of the same database.
In the attachment of a screenshot of the tables, you need to make a selection and show the names of goods, the names of subcategories and the names of categories.
Can you please tell me how to make a request?5cb8b2d0f2984678877262.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nozzy, 2019-04-18
@0ldn0mad

select
t1.name_goods,
t2.name_subcat,
t3.name_cat
from goods t1
left join subcategory t2 on t2.id_subcat = t1.id_subcat
left join category t3 on t3.id_cat = t1.id_cat

P
protsey, 2019-04-18
@protsey

Google towards JOIN. In this case INNER JOIN.

K
Konstantin Tsvetkov, 2019-04-18
@tsklab

You have an indeterminacy: in categoryno subcategoryfor id_subcat (110, 111).
In goodstrimmed id_cator not divided into subcategories.
And it is possible without correction:

SELECT goods.name_goods, subcategory.name_subcat, category.name_cat
  FROM goods 
    LEFT OUTER JOIN category ON goods.id_cat = category.id 
    LEFT OUTER JOIN subcategory ON goods.id_cat = subcategory.id_subcat / 10

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question