有没有更快的方法获取table1
中存在但table2
中不存在的id并将它们插入table2
中?
insert into table2 (id)
select id
from table1
where table1.id not in (select id from table2)
最佳答案
除了使用 in
运算符的解决方案之外,请尝试使用 exists
一个
select id
from table1 t1
where not exists (
select 1
from table2
where id = t1.id
)
如果子查询返回一个空集 not exists
计算结果为 true
外连接
select id
from
table1 t1
left join
table2 t2 on t1.id = t2.id
where t2.id is null
使用explain analyze
进行比较
关于sql - 将不匹配的 ID 插入列的更快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18156459/