My database has following structure:
Every Region (CountryStates) can have many cities (Areas). Every Area can have many tourist attractions or points of interest (POIs). I want to get list of how many Areas and Attractions are there in each Region. If there is none, I want to display 0.
This is my query:
SELECT Tab1.Reg AS Reg, CountAreas, CountPois
FROM
(SELECT
c.Name AS Reg,
COUNT(a.Id) AS CountAreas
FROM
CountryStates as c LEFT JOIN
Areas AS a ON a.CountryStates_Id = c.Id
GROUP BY c.Name
) as Tab1 left join
(SELECT
c1.Name AS Reg,
COUNT(p.Id) AS CountPois
FROM
CountryStates as c1 LEFT JOIN
Areas AS a ON a.CountryStates_Id = c1.Id LEFT JOIN
POIs AS p ON a.Id = p.Areas_Id
GROUP BY c1.Name
) as Tab2 on Tab1.Reg = Tab2.Reg
How can I make this query in just one SELECT?
This query returns the same result: