有三个表 表1: ID Name Counts... 1 AAA 2 BBB .... 表2: ID Name .... 1 AAA 2 AAA ... 表3: ID Name .... 1 BBB 2 BBB ... 现在想把表2和3中等于AAA和BBB的总数count放入到表1的Counts中,该怎么做? 换句话就是表1中的counts就是记录表2和表3的相应统计数。
SELECT ClassID,ClassName,
{case Belong '0' then SELECT COUNT(StuScore2002.ClassID) AS Amount
FROM Class INNER JOIN
StuScore2002 ON Class.ClassID = StuScore2002.ClassID
GROUP BY StuScore2002.ClassID, Class.ClassName
when '1' then SELECT COUNT(NewStu.ClassName) AS Amount
FROM Class INNER JOIN
NewStu ON Class.ClassName = NewStu.ClassName
GROUP BY NewStu.ClassName, Class.ClassName
end
}
as counts
FROM Class
--测试数据
declare @表1 table(id int,name varchar(10),counts int)
insert into @表1(id,name)
select 1,'AAA'
union all select 2,'BBB'
declare @表2 table(id int,name varchar(10))
insert into @表2
select 1,'AAA'
union all select 2,'AAA'
declare @表3 table(id int,name varchar(10))
insert into @表3
select 1,'BBB'
union all select 2,'BBB'
--方法1.
update @表1 set counts=b.ab
from @表1 a join (
select name=isnull(a.name,b.name),ab=sum(isnull(aa,0)+isnull(bb,0))
from(
select name,aa=count(*) from @表2 group by name
) a full join(
select name,bb=count(*) from @表3 group by name
) b on a.name=b.name group by isnull(a.name,b.name)
) b on a.name=b.name
--显示结果
select * from @表1
--方法2.
update @表1 set Counts=(select count(*) from
(select name from @表2 union all select name from @表3) aa where name=a.name)
from @表1 a
--显示结果
select * from @表1
--方法3.
update @表1 set counts=b.ab
from @表1 a join (
select name,ab=count(*)
from(select name from @表2 union all select name from @表3) a
group by name
) b on a.name=b.name
--显示结果
select * from @表1
/*--测试结果
--方法1
id name counts
----------- ---------- -----------
1 AAA 2
2 BBB 2
(所影响的行数为 2 行)
--方法2
id name counts
----------- ---------- -----------
1 AAA 2
2 BBB 2
(所影响的行数为 2 行)
--方法3
id name counts
----------- ---------- -----------
1 AAA 2
2 BBB 2
--方法1.
update 表1 set counts=b.ab
from 表1 a join (
select name=isnull(a.name,b.name),ab=sum(isnull(aa,0)+isnull(bb,0))
from(
select name,aa=count(*) from 表2 group by name
) a full join(
select name,bb=count(*) from 表3 group by name
) b on a.name=b.name group by isnull(a.name,b.name)
) b on a.name=b.name
--方法2.
update 表1 set Counts=(select count(*) from
(select name from 表2 union all select name from 表3) aa where name=a.name)
from 表1 a
--方法3.
update 表1 set counts=b.ab
from 表1 a join (
select name,ab=count(*)
from(select name from 表2 union all select name from 表3) a
group by name
) b on a.name=b.name
--方法2.
update 表1 set Counts=(select count(*) from
(select name from 表2 union al select name from 表3) aa where name=a.name)
from 表1 a
--方法3.
update 表1 set counts=b.ab
from 表1 a join (
select name,ab=count(*)
from(select name from 表2 union all select name from 表3) a
group by name
) b on a.name=b.name
--方法1.
update 表1 set counts=b.ab
from 表1 a join (
select name=isnull(a.name,b.name,ab=sum(isnull(aa,0)+isnull(bb,0))
from(
select name,aa=count(*) from 表2 group by name
) a full join(
select name,bb=count(*) from 表3 group by name
) b on a.name=b.name
) b on a.name=b.name
select *,
(case [name] 'AAA' then (select count(*) from 表2 where [name] = 'AAA') when 'BBB' then (select count(*) from 表3 where [name] = 'BBB') else 0 end)
as counts
from 表1