0

如何合并这 2 个表
我有这 2 个表:
Table1

ID      first_name      laste_name      colum1
1       John            Smith           CD
2       Foo             Bar             CD
3       Jean            Bon             CD
10      Super           Man             CD
11      Bat             Man             CD
12      Mr.             Green           CD

表2

ID      first_name      laste_name      colum2
1       John            Smith           MAG
2       Foo             Bar             MAG
3       Jean            Bon             MAG
4       James           Bond            MAG
5       Iron            Man             MAG
6       Sandra          Bullock         MAG

我想要这个结果:

表3

ID      first_name      laste_name      colum1      colum2
1       John            Smith           CD          MAG
2       Foo             Bar             CD          MAG
3       Jean            Bon             CD          MAG
4       James           Bond                        MAG
5       Iron            Man                         MAG
6       Sandra          Bullock                     MAG
10      Super           Man             CD
11      Bat             Man             CD
12      Mr.             Green           CD

关于如何做到这一点的任何想法?

4

1 回答 1

0

我正在使用mysql语法给出示例。

SELECT ifnull(A.Id,B.Id) ID,
   ifnull(A.Ifirst_name,B.first_name) AS first_name,   
   ifnull(A.laste_name,B.laste_name) AS laste_name, 
   A.Column1,b.Column2 
from Table1 A LEFT JOIN Table2 B on A.Id = B.Id

UNION ALL

SELECT ifnull(A.Id,B.Id) ID,
   ifnull(A.Ifirst_name,B.first_name) AS first_name,   
   ifnull(A.laste_name,B.laste_name) AS laste_name, 
   A.Column1,b.Column2 
from Table1 A RIGHT JOIN Table2 B on A.Id = B.Id
于 2013-07-26T11:33:10.880 回答