So a Country has many Regions (1-n) and a Service has one Region(1-1).
I would like to get all Countries only if they have active Services (services.active = 1)
Do you know how build this query ?
Thanks in advence.
ps: I could do this but I think is very ugly:
SELECT DISTINCT countries.id, countries.name FROM countries, regions, services
where countries.id = regions.country_id
and regions.id = services.region_id
Bearing the assumption that active is 0 or 1 this would do it:
SELECT
c.id
,c.name
FROM
countries c
INNER
JOIN
regions r
ON
c.id = r.country_id
INNER
JOIN
services s
ON
r.id = s.region_id
GROUP
BY
c.id
HAVING
MAX(s.active) = 1
There is no real need to have the HAVING clause, I would filter using in the WHERE clause (as this could potentially use indexes on the table):
SELECT DISTINCT c.id, c.name
FROM countries AS c
INNER JOIN regions AS r ON (c.id = r.country_id)
INNER JOIN services AS s USING (id)
WHERE s.active = 1;
I was thinking it would be nice to have all service data available just in case some other aggregate calculation is needed that might include inactive services. For example using my method it would be easy to count the number of active and inactive services per country.
my response was for the poor soul who suggested using the USING option
SELECT id
, name
FROM countries
WHERE EXISTS
( SELECT NULL
FROM regions
INNER
JOIN services
ON services.region_id = regions.id
AND services.active = 1
WHERE regions.country_id = countries.id )