table1に存在するが存在しないID を取得しtable2て挿入するより簡単な方法はありtable2ますか?
insert into table2 (id)
select id
from table1
where table1.id not in (select id from table2)
table1に存在するが存在しないID を取得しtable2て挿入するより簡単な方法はあり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
のouter join
select id
from
table1 t1
left join
table2 t2 on t1.id = t2.id
where t2.id is null
explain analyze比較に使用