如果一个人只能加入一个组队,可以在你的语句修改:SELECT inst, SUM(CASE WHEN team.student_code IS NULL THEN 0 ELSE 1 END) AS 人数
FROM student LEFT JOIN team
ON student.student_code = team.student_code
GROUP BY inst否则,应该:SELECT DISTINCT inst,ISNULL((SELECT COUNT(*) FROM team WHERE student_code = student.student_code),0) AS 人数
FROM student

解决方案 »

  1.   

    更正:否则,应该:SELECT DISTINCT inst,ISNULL((SELECT COUNT(DISTINCT student_code) FROM team WHERE student_code = student.student_code),0) AS 人数
    FROM student
      

  2.   

    SELECT inst, count(student.student_code)
    FROM student LEFT OUTER JOIN team
    ON student.student_code =team.student_code
    GROUP BY inst,student.student_code
      

  3.   

    正是左联接的作用,用left join on
      

  4.   

    你现在统计的是人数大于零的院系,剩下的就是人数为零的院系了。
    select a.* from 
    (SELECT inst, count(*) quantity
    FROM student, team
    WHERE student.student_code = team.student_code
    GROUP BY inst
    union all
    select inst,quantity=0 from student where inst not in (SELECT inst
    FROM student, team
    WHERE student.student_code = team.student_code
    ) ) a