2016-04-25 72 views
0


我有相同的列两个表我需要合并两个表像下面
按行加入两个表

Table1 

id name 

1 test1 
4 test7 
5 test9 
6 test3 


Table2  

id name 

2 test2 
3 test5 
6 test3 

Result 
id name 

1 test1 
2 test2 
3 test5 
4 test7 
5 test9 
6 test3 

所以我需要加入/合并由ID两个表,你可以看到在这两个表中的id 6我需要覆盖表2的值,并给出以上结果。请帮助我解决问题。
谢谢。

回答

0
select id,name from table1 
union 
select id,name from table2 ; 

其他方式

select * from (
select id,name from table1 
union 
select id,name from table2)temp order by temp.id ; 

这将安排记录ID明智

联盟将消除重复的记录,在你的情况下,它的ID 6

0

当你想要再排序必须是创建这样

select * from 
(
select id,name from table1 t1 
union  
select id,name from table2 t2 
)a order by a.id ; 
内查询