我们有4个简单的表格:
TABLE region (id, name)
TABLE country (id, name, region_id)
TABLE organization (id, name, country_id)
TABLE person (id, name, organization_id)
我想进行查询,我会在哪里:
例如: 欧洲,20,斯洛伐克,2,生命大学,1,某人
如何在MySQL中有效地进行这些计数?
我有一个巨大的查询,我在每个计数中都有内联选择,但这根本不是有效的。我正在考虑与案例相加,但如果我有数千个不同名称的组织,我就不太了解这个概念......
答案 0 :(得分:0)
SELECT r.name as region, count(DISTINCT r.id) as region_total,
c.name as city, count(DISTINCT c.id) as country_total,
o.name as organization, count(DISTINCT o.id) as organization_total,
COUNT(p.id) as person_total
FROM region r
JOIN country c
on r.id= c.region_id
JOIN organization o
ON c.id = o.country_id
JOIN person p
ON o.id p.organization_id
GROUP BY r.id, c.id, o.id
ORDER BY r.id, c.id, o.id
我不知道你想如何在这里加入一个人名