数据表KS_Article, KS_U_DemosService 及KS_U_English 具有四个相同的字段定义:id, title, Hits,FullTitle。

现为了查询各栏目的点击量,需要将上述表“合并”查询输出。

 

这里使用了临时表完成:

 

--如有相关临时表,先删除

IF object_ID('tempdb..#a') is not null    drop table #a
IF object_ID('tempdb..#b') is not null    drop table #b
IF object_ID('tempdb..#c') is not null    drop table #c

--查询并将相关数据插入临时表#a, #b, #c中

select top 200 id, title,Hits,FullTitle as '栏目' into #a from KS_Article order by hits desc
update #a set 栏目='信息中心'
select top 300 id, title,Hits,FullTitle as '栏目' into #b from KS_U_DemosService order by hits desc
update #b set 栏目='市民服务'
select top 50 id, title,Hits,FullTitle as '栏目' into #c from KS_U_English order by hits desc
update #c set 栏目='英文版'

--合并临时表数据并按点击量排序

select * from #a
union all select * from #b
union all select * from #c
order by Hits desc