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

use*_*083 4 sql

我有2个具有相同结构的SQL表.一个是第二个版本的更新版本.如何合并2以使新表的记录优先于另一个,并且仍然包含在较新表中没有更新的记录?

原始表ID(是主键):

ID, NAME, ADDRESS
11   AL    1 main street
22   BOB   2 main street
33   CHAZ  3 main street
Run Code Online (Sandbox Code Playgroud)

更新表

ID, NAME, ADDRESS
11  AL     99 maple street
22  BOB    2 main street
Run Code Online (Sandbox Code Playgroud)

结果我想要

ID, NAME, ADDRESS
11    AL   99 maple street
22    BOB  2 main street
33    CHAZ 3 main street
Run Code Online (Sandbox Code Playgroud)

谢谢,MC

jue*_*n d 5

coalesce将返回第一个非null值.与left join此结合使用将首先使用新数据,如果为null,则使用旧数据.

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
Run Code Online (Sandbox Code Playgroud)