我有两张桌子:
现在,我想列出父表中的行,其子行数为3。
答案 0 :(得分:1)
可以通过多种方式完成。最简单的可能是WHERE
子句中的相关子查询来计算子项数:
select *
from parents p
where (select count(*) from children c
where c.id_parent = p.id) = 3
或GROUP BY
,HAVING
:
select p.*
from parents p
join children c
on c.id_parent = p.id
group by p.id
having count(*) = 3
答案 1 :(得分:0)
我像这样编辑我的查询。
select p.*, count(c.id)
from parents p
join children c
on c.id_parent = p.id
group by p.id
having count(*) = 3
没关系? jarlh