I have a table listing states and one with cities, with State_number acting as a foreign key in the cities table. Is it possible to list the cities via their state name?
This is the closest I have gotten:
SELECT State_name, City_name
FROM States, Cities
JOIN Cities on States.State_number = Cities.State_number
WHERE States.State_name IN ('Munich', 'Brandenburg');
An example of the current output:
State_name
City_name
Bavaria
Munich
Bavaria
Nuremburg
Brandenburg
Berlin
Whereas I am hoping for something like:
Munich
7
Brandenburg
10
Just join them and apply a WHERE clause:
select st.state_name, c.city_name
from states s
join cities c on s.state_number = c.state_number
where s.state_name = 'Bavaria';
To get the count of cities per state (which wasn't part of the original question), you can use a group by
select st.state_name, count(*) as number_of_cities
from states s
join cities c on s.state_number = c.state_number
where s.state_name in ('Brandenburg','Bavaria')
group by st.state_name;
I think you want:
SELECT s.State_name, c.City_name
FROM States s JOIN
Cities c
ON s.State_number = c.State_number
WHERE c.City_name IN ('Munich', 'Brandenburg');
Notes:
FROM
clause.JOIN
syntax.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.