+ -
当前位置:首页 → 问答吧 → 多表最新记录

多表最新记录

时间:2011-12-02

来源:互联网

有T1~T6,想根据表中都有的列(pubdate)按从大到小取10条记录.说白了是从6张表中按日期取10条记录

作者: xiaofanku   发布时间: 2011-12-02

SQL code

--1

select top 10 公共字段 from t1 order by pubdate --desc  排序方式
union --all  可以取重复
select top 10 公共字段 from t1 order by pubdate --desc
union --all
...

--2
select top 10 *
from(
select 公共字段 from t1 
union --all  可以取重复
select 公共字段 from t1 
union --all
...
) t
order by pubdate  --desc

作者: AcHerat   发布时间: 2011-12-02

楼上是 t1 t2 。。

作者: AcHerat   发布时间: 2011-12-02

这我会!能给点其它的方案!不要视图和过程

作者: xiaofanku   发布时间: 2011-12-02

SQL code
select top 10 * from (select * from t1 union all select * from t2 union all select * from t3 select * from t4 union all select * from t5 union all select * from t6)t order by pubdate desc

作者: qianjin036a   发布时间: 2011-12-02

声明,上面是一条语句,不是视图也不是过程。

作者: qianjin036a   发布时间: 2011-12-02

引用 4 楼 qianjin036a 的回复:

SQL code
select top 10 * from (select * from t1 union all select * from t2 union all select * from t3 select * from t4 union all select * from t5 union all select * from t6)t order by pubdate desc

……

你写的和人家有什么不同

作者: xiaofanku   发布时间: 2011-12-02

SQL code
select top 10* from t1 order by pubdate desc
union all
select top 10* from t2 order by pubdate desc
...
select top 10* from t6 order by pubdate desc

作者: fredrickhu   发布时间: 2011-12-02

引用 6 楼 xiaofanku 的回复:
引用 4 楼 qianjin036a 的回复:

SQL code
select top 10 * from (select * from t1 union all select * from t2 union all select * from t3 select * from t4 union all select * from t5 union all select * from t6……

他写的是先子查询 再取前10 我们写的是分别取前10 再合并

作者: fredrickhu   发布时间: 2011-12-02

只能这一种方式么?再说是6张表中一共取10条记录不是从一张表中取10条记录

作者: xiaofanku   发布时间: 2011-12-02