如何合并/更新2个相同的SQL表

如何合并/更新2个相同的SQL表,sql,Sql,我有两个结构相同的SQL表。一个是第二个的更新版本。如何合并这两个表,使较新表的记录优先于另一个表,并且仍然包括较新表中没有更新的记录 原始表ID(是主键): 更新表 ID, NAME, ADDRESS 11 AL 99 maple street 22 BOB 2 main street 我想要的结果 ID, NAME, ADDRESS 11 AL 99 maple street 22 BOB 2 main street 33 CHAZ 3 main

我有两个结构相同的SQL表。一个是第二个的更新版本。如何合并这两个表,使较新表的记录优先于另一个表,并且仍然包括较新表中没有更新的记录

原始表ID(是主键):

更新表

ID, NAME, ADDRESS
11  AL     99 maple street
22  BOB    2 main street
我想要的结果

ID, NAME, ADDRESS
11    AL   99 maple street
22    BOB  2 main street
33    CHAZ 3 main street
谢谢,
MC

合并
将返回第一个非空值。结合
左连接
这将首先使用新数据,如果为空,则使用旧数据

select coalesce(u.id, o.id) as id,
       coalesce(u.name, o.name) as name,
       coalesce(u.address, o.address) as address
from original_table o
left join updated_table u on u.id = o.id
Try union:

Select id as uid, name, address from new_table
Union 
Select id, name, address from old_table
Where id not in (select id from new_table)
另外,尽量不要将id用作列名。好主意

UPDATE o
SET Name=u.Name
    ,Address=u.Address
from [original] o
inner join [updated] u on u.id = o.id
Select id as uid, name, address from new_table
Union 
Select id, name, address from old_table
Where id not in (select id from new_table)