我有10个表,我现在要统计每个表的条数分别显示在同一个页面上(这样的话就要连10次数据库),可不可以用视图把10个表统计出来的数据合在一个表里面这样前台操作就方便多了
解决方案 »
- 求sql行列转换
- 如何用T-SQL检查不同表间的对应字段,是否有主外键的关系
- |zyciis| 如何取出一条记录的ID号,同时更新他的操作状态Sel 而且在并发的时候不会出错重复读取 谢谢
- 好奇怪呀,为什么在同步时会出这个问题
- MS SqlServer Connection与Session的关系、区别
- 向树哥求救!因为没分了!所以树哥帮帮忙!
- 关于SQL运行速度的问题?
- 关于事务提交的问题(见笑)
- 如何使用Performance Monitor得到DBA所需的SQL Server性能指标
- 有种就把这个问题解决!!
- case when 条件查询
- pivottable 和 dso创建的数据挖掘模型有什么区别?
union
select 'table2' as tablename,count(*) as rcount from table2
union
select 'table3' as tablename,count(*) as rcount from table3
union
.....
from
(select count(*) as cnt from tb1) a1 ,
(select count(*) as cnt from tb2) a2 ,
(select count(*) as cnt from tb3) a3 ,
(select count(*) as cnt from tb4) a4 ,
(select count(*) as cnt from tb5) a5 ,
(select count(*) as cnt from tb6) a6 ,
(select count(*) as cnt from tb7) a7 ,
(select count(*) as cnt from tb8) a8 ,
(select count(*) as cnt from tb9) a9 ,
(select count(*) as cnt from tb10) a10
declare hcforeach cursor global for
select name from sysobjects where xtype='U' and name in(你需要显示的表)
exec sp_msforeach_worker 'select 表名=''?'',count(*) from ?'
col1=(select count(*) from table1),
col2=(select count(*) from table2),
col3=(select count(*) from table3),
col4=(select count(*) from table4),
col5=(select count(*) from table5), col6=(select count(*) from table6),
col7=(select count(*) from table7),
col8=(select count(*) from table8),
col9=(select count(*) from table9),
col10=(select count(*) from table10)
drop view mycnt
goCREATE VIEW mycnt
AS
select 说明 = '第一个表' , count(*) as 数量 from authors
union all
select 说明 = '第二个表' , count(*) as 数量 from employee
goselect * from mycntdrop view mycnt说明 数量
-------- -----------
第一个表 23
第二个表 43(所影响的行数为 2 行)
下一个是横向if object_id('pubs..mycnt') is not null
drop view mycnt
goCREATE VIEW mycnt
AS
select a.数量 as 第一表数量 , b.数量 as 第二表数量 from
(select count(*) as 数量 from authors) a,
(select count(*) as 数量 from employee ) b
goselect * from mycntdrop view mycnt第一表数量 第二表数量
----------- -----------
23 43(所影响的行数为 1 行)