(1) INSERT [INTO] B SELECT id, field1, field2 FROM A WHERE NOT EXISTS (SELECT id FROM B WHERE id=[A.]id)
(2) INSERT [INTO] B SELECT * FROM A WHERE id NOT IN (SELECT id FROM B)
---------------------------------------------------------------------------
这两句,都是将 A 表中存在, 但B表中不存在的数据, 插入到B表中, 关联比较字段为 id.
但这两句的执行效率,却是有数量级的差别.
结论有两点:
尽量用 EXISTS 和 NOT EXISTS 代替 IN 和 NOT IN 不要偷懒, 尽量不用 SELECT * FROM ...., 而要写字段名 SELECT field1,field2,....本文地址:http://com.8s8s.com/it/it20708.htm