有表t1(订单号,金额,流水号,创建时间,状态……)
表t2(订单号,金额,流水号,创建时间,状态……)要把 t1 表的数据 全部插入到t2 ,但是如果 订单号、金额、流水号 ’同时相等‘ ,此条跳过要怎么完成???sql语句 存储过程都行

解决方案 »

  1.   

    insert into t2 select *  from t1 not exists (select 1 from t2)
      

  2.   

    insert t2 select * from t1 except select * from t2  --该种写法 两表字段数量必须相等(如果有自增ID ,最好将具体的列 列出)
    --或者
    insert t2 select * from t1 not exists ( select * from t2 where t2.订单号=t1.订单号 and t2.金额=t1.金额 and t2.流水号=t1.流水号)
      

  3.   

    insert into t2 select *  from t1 not exists (select 1 from t2 where t1.订单号=t2.订单号  and t1.金额=t2.金额 and t1.流水号=t2.流水号)
      

  4.   

    用这种
    insert t2 select col1,col2,col3... from t1 not exists ( select 1 from t2 where t2.订单号=t1.订单号 and t2.金额=t1.金额 and t2.流水号=t1.流水号)
      

  5.   


    正确!!!再问下,类似的这样合并操作,有没有被的办法,就是不用Exists的,一直不会Exists,平时都比较回避它。
      

  6.   

    excepte这个关键字你到联机丛书查查能不能用。not exists的效率没这个好。具体用法忘了