数据表结构如下:
A              B              C             D             E
------------------------------------------------------------
123            25             11            11            null
123            85             21            21            null
456            25             8             8             null我想将C、D相加并写入E,这样的SQL语句怎么写?

解决方案 »

  1.   

    select c,d,e=c+d from 表
      

  2.   

    update [Table] set E=c+d
      

  3.   

    update 表名 set e=c+d
      

  4.   

    或建表时就约定e=c+d
    例:create table table67(c int,d int,e as c+d)
      

  5.   

    如果我要加查询条件呢,比如要更新的数据条件需要用下面的语句先查询出来SELECT table1.*
    FROM table2 INNER JOIN
          table1 ON table2.Barcode = table1.Barcode 
    where table2.w = '123'
      

  6.   

    ??
    select table.*,convert(decimal(20,6),c+d) e 
    FROM table2 INNER JOIN
    table1 ON table2.Barcode = table1.Barcode 
    where table2.w = '123'
      

  7.   

    我的意思是先按照这条语句把记录查询出来
    SELECT table1.*
    FROM table2 INNER JOIN
          table1 ON table2.Barcode = table1.Barcode 
    where table2.w = '123'然后操作查询出来的记录,将table1.C + table1.D写入table1.E
      

  8.   

    update table1 set e=convert(decimal(10,6),c+d) from table2 where table2.Barcode = table1.Barcode and table2.w='123'
      

  9.   

    另外我想限定计算结果的位数,譬如计算出来的值小数点后保留6位
    ----------------
    首先要保证你的c d e字段类型是可有小数的 例如float numeric decimal