I have three tables in a Mysql database – countries, cities and hotels. Their relations are: countries hasMany cities and cities hasMany hotels.
I want to be able to select all hotels given a certain countries.id. I am pretty weak in SQL. The join statement i came up with is as follows which always returns 0 rows.
SELECT
countries.name as country,
cities.name as city,
hotels.name as hotel
FROM
countries
left join cities
on countries.id = cities.country_id
left join hotels
on cities.id = hotels.city_id
WHERE
countries.id = @id
help?
Try leaving off the where clause and see what records you actually have. Maybe the specific id you’re using is not a known country id.
Also, just on a strictly formatting/style issue, IMHO this is better: