求sql语句,望大侠们帮忙
比如有两个表a和b
表a的字段aId与表b的字段aId关联
表a:
字段: aId name
1 牛
2 马
表b:
字段: aId time
1 12
1 3
2 7
1 9
用什么SQL查询语句可统计:
aId name 统计
1 牛 3
2 马 1
------解决方案--------------------select
a.aId,
a.name,
isnull(count(b.aid),0) 统计
from
a
left join
b
on
a.aId=b.aId
group by
a.aId,a.name
------解决方案--------------------select a.aId,name,统计=count(1) from a,b where a.aId =b.aId group by a.aId,name
------解决方案--------------------select t.*,(select count(*) from b where aId=t.aId) 统计
from a t
--or:
select t.*,m.统计 from a t
left join
(select aId,count(*) 统计 from b group by aId) m
on t.aId=m.aId