0

标题有点说明了一切。

我有 2 个表都有匹配的数据

Table Name: Customers
_____________________________
ID   | CompanyName
--------------
1    | Joes
2    | Kennys
3    | Kellys
4    | Ricks
5    | Johns


Table Name: OldCustomers
_____________________________
ID   | CompanyName
--------------
1    | Joes
2    | Kennys
3    | Kellys
4    | Ricks

我想在两个表之间进行比较。然后取出表 2 中不存在的行,并将其添加到我创建的名为“NewCustomers”的表中

4

1 回答 1

2

您可以使用以下内容:

insert into NewCustomers(id, companyname)
select c.id
  , c.companyname
from Customers c 
left join OldCustomers oc
  on c.companyname = oc.companyname
where oc.id is null;

select *
from NewCustomers

请参阅带有演示的 SQL Fiddle

此查询将查找 OldCustomers 表中缺少公司名称的所有记录。如果您想同时加入 id 和公司名称,您只需添加and c.id = oc.idleft join. 它会给你同样的结果。

于 2012-07-10T22:02:13.180 回答