注册 登录
编程论坛 SQL Server论坛

这个连接查询怎么做?

cobby 发布于 2008-03-19 11:05, 1028 次点击
有表A和表B
A为
id     name    age
1       ab      20
2       cd      21
……

B为
id    course   level
1       1       1
1       1       2
1       2       3
2       1       1
2       3       1

现在我想查询A表所有列,以及A表中每个id对应课程( course)为“1”的数量,重新组成一张表,即
id     name    age    num
1        ab    20      2
2        cd    21      1

请问这种查询应该怎么做?
5 回复
#2
stonefang2008-03-19 11:38
老大,你的表没用主键啊?
#3
cobby2008-03-19 11:40
select a.id,a.name,a.age,count(*) from a,b
where a.id=b.id and b.course=1
group by a.id,a.name,a.age

已经搞定了
#4
cobby2008-03-19 11:40
复合主键
#5
sunkaidong2008-03-19 17:25
先分组在having不也可以吗?
#6
yuwenxiang2008-03-27 19:31
select a.*,count(*) num from a,b
where a.id=b.id and b.course=1
group by sid
1