D
D
Dmitry Skogorev2015-09-28 00:17:56
MySQL
Dmitry Skogorev, 2015-09-28 00:17:56

How to join multiple tables?

Good day. Previously, I had minimal contact with join and I ask you to tell me how to make such a request or change the architecture.
there is a table regions
id, name
1, Moscow
2, St. Petersburg
3, Saratov
table cities
id, parentid, name
10, 1, Reutov
11, 2, Shushary
12, 3, Engels
and, accordingly, a table with my data
id data, location, name
1, 1, Ivan
2, 3, Stepan 3
, 11, Peter Those. At the exit, get 1, Moscow, Ivan 2, St. Petersburg, Stepan 3, Shushary, Petr
thanks in advance for the replies

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Fayozzhon Berdiev, 2015-09-28
@CybernatiC

SELECT * FROM appusers a LEFT JOIN city c ON a.city = c.id_city LEFT JOIN country cntry ON c.id_country = cntry.id_country

T
Therapyx, 2015-09-28
@Therapyx

Don't forget to make clear names for your tables. Region(RegionID, Name); city(CityID, RegionID, CityName) etc. to make it clear what is foreignkey and what is not.
Location where these numbers come from, what is it? Region? City? I understand I want to be shorter, but then there are more problems. Especially for people who can work with it in the future. And Berdiev has already given a good example)

E
evilelf, 2015-09-28
@evilelf

if the location in data is cities, you can try this:

SELECT r.name, d.* FROM data AS d INNER JOIN cities AS c ON c.id=d.location INNER JOIN regions AS r ON r.id = c.parentid

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question