SQL query in two tables
I have two tables:
Table: cities
city_id, lat, lon, name, country_id
Table: Countries
country_id, name
I want the SQL query to return:
cities.lat, cities.lon, cities.name, countries.name(for the corresponding cities.country_id)
I swear it should be easy. But I cannot get it to work.
+3
source to share
5 answers
If I got your point, this is a request.
SELECT cities.lat, cities.lon, cities.name, countries.name
FROM cities INNER JOIN countries
ON cities.country_id = countries.country_id
By the way, thought you can see what is cities.country_id
referring to countries.country_id
, you should have stated it explicitly.
0
source to share
This is how I solved it using a subquery. But use the answer given by Ajay2707 and marc_s above. It gives the same result. I've put my answer just for reference (subquery) here :)
SELECT cities.name AS city, cities.lon, cities.lat, (SELECT countries.name AS country FROM countries WHERE countries.country_id=cities.country_id ) AS country FROM cities
0
source to share