日期:2014-05-18  浏览次数:20528 次

两个表合并显示
表a 

id title addtime
1 aaa 2007-02-23
2 bbb 2007-06-05

表b

id title addtime
1 assf 2007-03-23
2 dfsd 2007-05-05

现在想把连个表合并显示按时间倒序排列,输出结果为:

addtime title

2007-06-05 bbb  
2007-05-05 dfsd  
2007-03-23 assf  
2007-02-23 aaa  






------解决方案--------------------
select addtime,title from (
select addtime,title from a 
union all
select addtime,title from b ) as c
order by addtime desc

------解决方案--------------------
select addtime,title from a
union all
select addtime,title from b order by addtime desc
------解决方案--------------------
select addtime,title from (select * from a union all select * from b)t
order by addtime desc
------解决方案--------------------
select addtime , title from a
union all
select addtime , title from a
order by a.addtime desc
------解决方案--------------------
SQL code
create table a(id int,title varchar(10),addtime datetime)
insert into a values(1,     'aaa',      '2007-02-23') 
insert into a values(2,     'bbb',      '2007-06-05')
create table b(id int,title varchar(10),addtime datetime)
insert into a values(1,     'assf',     '2007-03-23')
insert into a values(2,     'dfsd',     '2007-05-05')
go

select addtime , title from a 
union all 
select addtime , title from b
order by a.addtime desc

drop table a,b


/*
addtime                                                title      
------------------------------------------------------ ---------- 
2007-06-05 00:00:00.000                                bbb
2007-05-05 00:00:00.000                                dfsd
2007-03-23 00:00:00.000                                assf
2007-02-23 00:00:00.000                                aaa

(所影响的行数为 4 行)
*/

------解决方案--------------------
SQL code
select addtime,title from a 
union all 
select addtime,title from b order by addtime desc

------解决方案--------------------
使用 union
------解决方案--------------------
SQL codeselect addtime,title from a 
union all 
select addtime,title from b order by addtime desc