如何将不同的记录从一个表追加到另一个表 [英] How to append distinct records from one table to another

查看:58
本文介绍了如何将不同的记录从一个表追加到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当主表可能有重复项时,如何仅将主表中的不同记录追加到另一个表中。示例-我只希望较小的表中有不同的记录,但我需要将记录插入/追加到较小的表中已有的记录。

How do I append only distinct records from a master table to another table, when the master may have duplicates. Example - I only want the distinct records in the smaller table but I need to insert/append records to what I already have in the smaller table.

推荐答案

忽略任何并发问题:

insert into smaller (field, ... )
select distinct field, ... from bigger
except
select field, ... from smaller;

您也可以将其重新命名为联接:

You can also rephrase it as a join:

insert into smaller (field, ... )
select distinct b.field, ... 
from bigger b
left join smaller s on s.key = b.key
where s.key is NULL

这篇关于如何将不同的记录从一个表追加到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆