Sample Header Ad - 728x90

Selecting related data without selecting foreign key from subquery

1 vote
1 answer
216 views
cities | id | name | county_id | |----|:------:|----------:| | 1 | City1 | 1 | | 2 | City2 | 1 | | 3 | City3 | 2 | | 4 | City4 | 2 | counties | id | name | |----|:-------:| | 1 | County1 | | 2 | County2 | Hello,
I would like to select all city names from related county by enetring a city name.
I can do it via subquery, but is there any other way to achieve this?
SELECT name
FROM cities
WHERE cities.county_id =
      (SELECT county_id
       FROM cities
       WHERE name = 'City1'
       LIMIT 1);

result => "City1, City2"
Thank you.
Asked by rjeremy (13 rep)
Sep 9, 2021, 12:08 PM
Last activity: Jun 20, 2025, 10:51 AM