日期:2014-05-17  浏览次数:20465 次

2表联合查询
目地是查询所有课程及选课人数

有2表
tableA,
ID          NAME1 
1           语文
2           数学
3           英语
tableB
NAME2    NAME1  
张三     语文
李四     语文
赵六     数学

查询结果为
课程    人数
语文    2
数学    1
英语    0
select A.NAME1 课程,count(NAME2) 人数 from A,B group by B.NAME1
不知道这样可不可以实现。

------解决方案--------------------
select a.name1 课程,sum(case when isnull(name2,'')!='' then 1 else 0 end)人数 from tablea a left join tableb b on a.name1=b.name1 group by a.name1

------解决方案--------------------
select name1 课程,isnull((select count(name1) from tableB where tableB.name1=tableA.name1 group by name1),0) 人数 from tableA