如何合并这两个表
我有这两个表:
表1
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
关于如何做到这一点的任何想法?
答案 0 :(得分: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