我想插入table1.id
到table2.t1col
只有当table1.id
不存在table2.t1col
,但。
我想我必须使用:
insert into table2 name (t1col) value (select id from table1)
但是我只想添加一个id
不存在的东西table2
。
唯一性/索引约束保证了值的唯一性。因此,建议。
但是,不幸的是,违反约束会导致整体insert
失败。因此,您可以执行以下操作:
insert into table2(t1col)
select id
from table1 t1
where not exists (select 1 from table2 t2 where t2.t1col = t1.id);
您还应该具有唯一的索引/约束,以防止将来出现问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句