我有 3 个这样的表:
Areas
id city_id area
1 1 mayfield
2 2 cleveland
neighbourhood
id neighbourhood area_id
101 cayahoga 1
102 milan 1
103 downtown 2
Salons
id neighbourhood_id
3 101
4 101
5 102
我正在尝试统计所有区域的所有沙龙。
喜欢地区的沙龙(梅菲尔德 = 2,克利夫兰 = 1)
是否可以在 1 个查询中完成此操作?
最佳答案
SELECT areas.area,COUNT(Salons.id)
FROM areas
LEFT JOIN neighbourhood ON neighbourhood.area_id = areas.id
LEFT JOIN Salons ON Salons.neighbourhood_id = neighbourhood.id
GROUP BY areas.id
关于mysql 查询从表中获取计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27150235/