比如:
INSERT INTO archivetitles
(title_id, title, type, pub_id)
SELECT title_id, title, type, pub_id
FROM titles
WHERE (pub_id = '0766')
INSERT INTO archivetitles
(title_id, title, type, pub_id)
SELECT title_id, title, type, pub_id
FROM titles
WHERE (pub_id = '0766')
select 1 from 目标表
的记录集
select ... from 源表 a
where not exists (
select 1 from 目标表
where key=a.key
)从源表中,查询出在目标表中没有的记录,查询条件为key相等
然后将查询出的结果插入目标表一次性插入,不是一条,也不是整个表. 没有删除的慨念.
key是字段名
a.key表示是别名为a的源表里的key字段
insert 目标表 values (1)
select * from 目标表 ---目标表开始的数据create table 源表( [key] int )
insert 源表 values (1)
insert 源表 values (2)
insert 源表 values (3)select * from 源表 --源表数据insert 目标表([key])
select [key] from 源表 a
where not exists (
select 1 from 目标表
where [key]=a.[key]
)select * from 目标表 ---执行完语句目标表的数据drop table 目标表,源表---结果:
(所影响的行数为 1 行)key
-----------
1(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)key
-----------
1
2
3(所影响的行数为 3 行)
(所影响的行数为 2 行)key
-----------
1
2
3(所影响的行数为 3 行)这样你就看明白了吧