当前位置: 代码迷 >> Sql Server >> 两个表合并显示,该如何处理
  详细解决方案

两个表合并显示,该如何处理

热度:190   发布时间:2016-04-27 19:25:38.0
两个表合并显示
表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')goselect addtime , title from a union all select addtime , title from border by a.addtime descdrop table a,b/*addtime                                                title      ------------------------------------------------------ ---------- 2007-06-05 00:00:00.000                                bbb2007-05-05 00:00:00.000                                dfsd2007-03-23 00:00:00.000                                assf2007-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

  相关解决方案