在sql中合并和消除两个表的重复数据
我需要合并两个表。想法是将表b的分数用作默认值,只有表a中具有相同组pid但分数不同的行添加到表b中 表a在sql中合并和消除两个表的重复数据,sql,Sql,我需要合并两个表。想法是将表b的分数用作默认值,只有表a中具有相同组pid但分数不同的行添加到表b中 表a group pid score country -------------------------------------- T1 aa 10.1 US T1 aa 10.1 FR T1 aa 10.1 UK T1 aa 10.1 CS T1 aa 16.2 BR 表b group
group pid score country
--------------------------------------
T1 aa 10.1 US
T1 aa 10.1 FR
T1 aa 10.1 UK
T1 aa 10.1 CS
T1 aa 16.2 BR
表b
group pid score country
--------------------------------------
T1 aa 10.1 Default
预期结果:
group pid score country
--------------------------------------
T1 aa 10.1 Default
T1 aa 16.2 BR
一种方法是
联合所有
和不存在
:
select b.*
from b
union all
select a.*
from a
where not exists (select 1
from b
where b.group = a.group and
b.pid = a.pid
b.score = a.score
);