T
T
tolsor2018-05-09 03:46:15
MySQL
tolsor, 2018-05-09 03:46:15

How to display multiple cells from different tables with different conditions?

You need to display the full address of the student Petrov (region, city, street, house and apartment).
I can output one column like "Region" like in the following query. And how to bring the rest after him?

SELECT `nazvanie` AS 'Регион' 
FROM `region` 
WHERE `kod_region` = ( SELECT `kod_region` FROM `gorod` WHERE `kod_gorod` 
                                 = ( SELECT `kod_gorod` FROM `ulica` WHERE `kod_ulica` 
                                 = ( SELECT `kod_ulica` FROM `dannie` WHERE `fam` = 'Петров')))

Database Schema:
5af244b6725f7207421974.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2018-05-09
@tolsor

Try something like this:

SELECT `nazvanie` ,  `gorod`,  `ulica` 
FROM  `dannie` as D LEFT JOIN `ulica` as U  on D.`kod_ulica` = U.`kod_ulica`
                    LEFT JOIN `gorod` as G  on U.`kod_gorod` = G.`kod_gorod`
                    LEFT JOIN `region` as R  on G.`kod_region` = R.`kod_region`

But it might be better to have one address table, with columns: region code, city code, street code, so as not to glue the tables on the go.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question