select a.id, a.schoolname, 
sum(case when b.id is not null then 1 else 0 end ) as '学校数量',
sum(case when b.id is not null then 1 else 0 end ) as '评论数量'
from 学校表 a left join 学生表 b on a.id=b.schoolid
left join 评论表 c on a.id=c.schoolid
group by a.id, a.schoolname

解决方案 »

  1.   

    select a.id, a.schoolname, 
    count(b.ID) as '学校数量',
    count(c.ID) as '评论数量'
    from 学校表 a 
    left join 学生表 b on a.id=b.schoolid
    left join 评论表 c on a.id=c.schoolid
    group by a.id, a.schoolname
      

  2.   

    select id, schoolname, 
        ( select count(*) from student where schoolid = a.id ) as countstudent,
        ( select count(*) from comment where schoolid = a.id ) as countcomment
    from school a