合并不同表中的两列

时间:2015-05-11 17:08:01

标签: mysql

我有两张桌子,

项目

-----------------------
id  | name | created
-----------------------
1   | p1   | 2015-05-05
2   | p2   | 2015-04-29
3   | p3   | 2015-05-07

任务

------------------------
id  | name   | created
------------------------
1   | t1     |2015-05-04
2   | t2     |2015-04-30
3   | t3     |2015-05-06

我想要这张桌子。

上次行动

--------------------------
type |name    | created
--------------------------
p    | p3     | 2015-05-07
t    | t3     | 2015-05-06
p    | p1     | 2015-05-05
t    | t1     | 2015-05-04
t    | t2     | 2015-04-30
p    | p2     | 2015-04-29

p - 项目类型和t - 任务类型

如何获取此数据?

1 个答案:

答案 0 :(得分:2)

也许最简单的方法是union all

select * from
(
 select 'p' as type,
 name,
 created 
 from Project
 union all
 select 't' as type,
 name,
 created 
 from Task
)x
order by created desc