如何插入条件为 "if not exists" 的数据

How to insert data with condition "if not exists"

我只想插入 B 中 Role||Email 与 A 中已存在的 Role||Email 不匹配的行,而忽略其余部分(如 NAME)。

我的表是:

我尝试了以下方法:

insert into A (NAME, ROLE, EMAIL) 
SELECT NAME, ROLE, EMAIL from B 
where NOT EXISTS (SELECT ROLE, EMAIL FROM B WHERE A.NAME = B.NAME AND A.EMAIL = B.EMAIL);

虽然不起作用,我该如何解决?

insert into A (NAME, ROLE, EMAIL)
select B.NAEM,B.ROLE,B.EMAIL from B left outer join A 
on B.ROLE=A.ROLE and B.EMAIL=A.EMAIL
where A.ROLE is null

确保您的 ROLE 列中没有空值,如果是这样,请将 A.ROLE is null 更改为 (NAME, ROLE, EMAIL) 中永远不会为空的任何列。