W
W
WinStar2015-04-25 06:48:04
MySQL
WinStar, 2015-04-25 06:48:04

How to correctly create a request for selecting the country of loading and unloading?

The database has 2 tables.
1 table. With the names of all countries countries ((PK)id, country_name)
2 table. With transport orders orderdetails ((PK)ID, ... , (FK)load_country_id, (FK)unload_country_id) which stores the loading and unloading country id.
How to correctly create a SELECT query so that it would display the names of the country of loading and unloading?
Complicated option:
There are 3 tables in the database.
1 table. With the names of all countries countries ((PK)id, country_name)
2 table. With the names of all cities cities ((PK)id, (FK)country_id, city_name) which is linked to the table of all countries.
3 table. With transport orders orderdetails ((PK)id, ... , (FK)load_country_id, (FK)unload_country_id, (FK)load_city_id, (FK)unload_city_id) which stores the country id and city id of loading and unloading.
How to correctly create a SELECT query so that it would display the names of the country and city of loading and unloading?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Inv3go, 2015-04-25
@WinStar

SELECT o.id, c.country_name as load_country,c1.country_name as unload_country FROM `orderdetails` as o LEFT JOIN `countries` as  c ON o.load_country_id = c.id
LEFT JOIN `countries`  as c1 ON o.unload_country_id = c1.id;

SELECT o.id, c.country_name as load_country,c1.country_name as unload_county,ci.city_name as load_city,ci1.city_name AS unload_city FROM `orderdetails` as o 
LEFT JOIN `countries` as  c ON o.load_country_id = c.id
LEFT JOIN `countries`  as c1 ON o.unload_country_id = c1.id
LEFT JOIN `cities` as ci ON o.load_city_id = ci.id
LEFT JOIN `cities` as ci1 ON o.unload_city_id = ci1.id;

R
Rsa97, 2015-04-25
@Rsa97

If you want to be done for you - go to Freelance .
If something does not work, write what you have already done and what is wrong.

W
WinStar, 2015-04-25
@WinStar

Here's what I did:
The query returns the country and city of the download

SELECT countries.country_name AS "Страна загрузки", cities.full_name_nd AS "Город загрузки" 
FROM orderdetails INNER JOIN countries INNER JOIN cities 
WHERE orderdetails.load_country_id = countries.id AND orderdetails.load_city_id = cities.id

The request returns the country and city of unloading
SELECT countries.country_name AS "Страна выгрузки", cities.full_name_nd AS "Город выгрузки" 
FROM orderdetails INNER JOIN countries INNER JOIN cities 
WHERE orderdetails.unload_country_id = countries.id AND orderdetails.unload_city_id = cities.id

The gag is that I don't know how to combine these two queries into one

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question