34,575
社区成员
发帖
与我相关
我的任务
分享
select a.questionid,count(b.answser) cnt
from questions a
join answsers b
on a.questionid=b.questionid
group by a.questionid
create table a (id int identity(1,1),question varchar(36))
go
create table b (id int identity(1,1),aid int,answer varchar(36))
go
insert into a
select '中国的首都是哪个城市?' union all
select '你叫什么?'
insert into b
select 1,'上海' union all
select 1,'天津' union all
select 1,'深圳' union all
select 1,'北京' union all
select 2,'王五' union all
select 2,'张三' union all
select 2,'李四'
select * from a
select * from b
select a.question 问题,count(*) 答案数 from a inner join b on a.id=b.aid group by question
select 问题表.问题,isnull(count(答案表.答案ID),0) from 问题表 left join 答案表
on 问题表.问题ID = 答案表.问题ID