首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

多表查询的有关问题

2012-02-24 
多表查询的问题现在有我多个表表的结构id(int)bt(varchar)cs(int)有10多个表,表的结构都一样现在ID是自动

多表查询的问题
现在有我多个表
表的结构
id(int)       bt(varchar)     cs(int)
有10多个表,表的结构都一样
现在ID是自动标示
cs是写入不同的数
我现在想查询这10多个表cs字段共和,条件是bt=XX
要怎   么实现啊




[解决办法]
select sum(cs) cs的和 from
(
select * from tb1
union all
select * from tb2
...
union all
select * from tb10
) t
where bt = 'xx '
[解决办法]
create table #T(id int,bt varchar(50),cs int)
insert into #T
select id,bt,cs from table1 union all
select id,bt,cs from table2 union all
……………………………………………………
select id,bt,cs from tableN

select sum(cs) from #T where bt=XX group by bt
[解决办法]
select bt,sum(cs) from
(
select * from tb1
union all
select * from tb2
...
union all
select * from tb10
) t
where bt = 'xx '
group by bt
[解决办法]
id(int) bt(varchar) cs(int)
有10多个表,表的结构都一样
现在ID是自动标示
cs是写入不同的数
我现在想查询这10多个表cs字段共和,条件是bt=XX
--------------------------------------------
select (sum(a.cs)+sum(b.cs)+sum(c.cs)+.......+sum(j.cs)) as totalCount
from tab1 a full join tab2 b on a.id=b.id..........full join tab10 j on a.id=j.id
where a.bt=XX and b.bt=xx and ......j.bt=xx

热点排行