[英]MySQL subquery with two tables count and group by
你可以用基本相同的結構做你想做的事:
Select h.town, count(*)
from hospital h
where h.id in (select hd.hospital_id
from hospital_doctor hd
group by hd.hospital_id
having count(*) > 5
)
group by h.town ;
請注意以下事項:
in
not =
使用,因為子查詢可能返回多於一行。Select hospital.town, count(town)
from hospital
where
hospital.id in (
select hospital_id
from hospital_doctor
group by hospital_id
having count(hospital_id)>5
)
group by town
SELECT h.town, count(h.town)
FROM
(
SELECT hospital_id
FROM hospital_doctor GROUP BY hospital_id
HAVING count(doctor_id)>5
) s1
left outer join hospital h on (s1.hospital_id=h.id)
GROUP BY h.town
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.