现在的位置: 首页 > 综合 > 正文

SQl聚合函数sum 和count

2012年10月09日 ⁄ 综合 ⁄ 共 359字 ⁄ 字号 评论关闭
select t1.teacherId,t1.teacherName,sum(case when t2.teacherId=t1.teacherId then 1 else 0 end) as Num
from testTeacher as t1,testStudent as t2

group by t1.teacherId,t1.teacherName
order by t1.teacherId

select t.teacherId,t.teacherName,isnull(counts,0) as stuNum from testTeacher as t
left join
(
select count(teacherId) counts,teacherId from testStudent
group by teacherId
) as c on t.teacherId=c.teacherId

抱歉!评论已关闭.