比如:
INSERT INTO archivetitles 
   (title_id, title, type, pub_id)
SELECT title_id, title, type, pub_id
FROM titles
WHERE (pub_id = '0766')

解决方案 »

  1.   

    哪么select 1 from 目标表是什么??
      

  2.   

    从原表中排除
    select 1 from 目标表
    的记录集
      

  3.   

    insert 目标表(...)
    select ... from 源表 a
    where not exists (
    select 1 from 目标表
    where key=a.key
    )从源表中,查询出在目标表中没有的记录,查询条件为key相等
    然后将查询出的结果插入目标表一次性插入,不是一条,也不是整个表. 没有删除的慨念.
      

  4.   

    你可先不看insert,就是一个典型的not exists子查询,再看insert其意自明!
      

  5.   

    a是源表的别名
    key是字段名
    a.key表示是别名为a的源表里的key字段
      

  6.   

    create table 目标表( [key] int )
    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 行)这样你就看明白了吧