A
A
Alexey Fr2015-02-25 18:02:05
SQL
Alexey Fr, 2015-02-25 18:02:05

How to make sql query from four tables?

Bottom line: select all users -> take all data from the user -> select a city whose id is equal to city_id in the user table -> select the names of cities, regions and countries -> ids of countries and regions are indicated in the city table. Below, I kind of wrote about it, but I want it in one request.
SELECT * FROM users
SELECT name FROM city WHERE user.city_id = city
SELECT name FROM region WHERE city.region_id = region_id
SELECT name FROM country WHERE city.country_id = country_id

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wscms, 2015-02-25
@Warrangie

select u.*, c1.name as city_name, c2.name as country_name, r.name as region_name from users u
join city c1 on u.city_id = c1.city
join region r on c1.region_id = r.region_id
join country c2 on c1.country_id = c2.country_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question