27,579
社区成员
发帖
与我相关
我的任务
分享
create table #a (id int identity(1,1),aname varchar(20),flag int)
insert into #a
select 'jack', 1 union
select 'marky', 1 union
select 'jodarn', 2
create table #b (id int identity(1,1),flag varchar(20),chid int)
insert into #b
select 1, 1 union
select 2, 2 union
select 3, 3
-----------
select id,flag,jieguo=(select count(1) from #a where #a.flag=#b.flag) from #b
/*
id flag 统计的结果
--- ---- ---------------
1 1 2
2 2 1
3 3 0
*/
select
b.id,b.flag,count(1) as 结果
from
b join a
on
b.flag=a.flag
group by
b.id,b.flag
select B.id,B.flag,统计的结果=count(1)
from B ,A
where B.flag=A.flag
group by B.id,B.flag
select a.id,a.flag,count(*) as 统计的结果 from
(
select b.* ,a.id as aid from B b left join A a on b.flag = a.flag
) a group by a.id,a.flag