insert 表 (year,month,a,b,c)
select year,4 month,a,b,c from 表 where month=3
except
select year,month,a,b,c from 表 where month=4

解决方案 »

  1.   


    你好,如果表中
    year    month   a       b           c         d
     2014     3         a1      b1       c1       10
     2014     3         a2      b2       c2        111
     2014     3         a3      b3       c3        41
     2014     4         a1      b1       c1         55还是已 a  b   c 三列判断,但是d列也要插入
    怎么写呢?谢谢
      

  2.   


    insert 表 (year,month,a,b,c,d)
    select year,4 month,a,b,c,d from 表 t where month=3
      and not exists
    (
          select year,month,a,b,c
          from 表
          where year=t.year and month=4 and a=t.a and b=t.b and c=t.c
    )
      

  3.   

    太简单了,一条语句搞定UPDATE T SET month=4 FROM Table T
    WHERE NOT EXISTS (
    SELECT a,b,c FROM Table S 
    WHERE S.month=4 AND S.a=T.a AND S.b=T.b AND S.c=T.c
    )