有两个表A:ID,GZ
B:BM,GZ
我现在想要求修改A表中ID为5的记录的GZ字段,使其等于B表中所有BM='AAAA'的记录的GZ字段的和.
谢谢

解决方案 »

  1.   

    update a set gz=(select sum(gz) from B where BM='aaaa') where id=5
      

  2.   

    有两个表A:ID,GZ
    B:BM,GZ
    我现在想要求修改A表中ID为5的记录的GZ字段,使其等于B表中所有BM='AAAA'的记录的GZ字段的和.
    谢谢方法1:
    update 表A set GZ=(select sum(GZ) 表B where BM='AAAA') where ID=5方法2:
    declare @GZ int
    select @GZ = sum(GZ) from 表B where BM='AAAA'
    update 表A set GZ=@GZ where ID=5
      

  3.   

    update a set gz=(select sum(gz) from b where bm='aaaa') where id=5
      

  4.   

    但这样做有一个问题,如果B表没有合适记录会存进NULL.请问是否可设为如果没有合适记录也存0呢?
      

  5.   

    给它设置一个默认值,如果为null时就为0
      

  6.   

    "给它设置一个默认值,如果为null时就为0"
    您是说在A表的字段属性中设为默认值为0吗?
    我设了啊,我的GZ字段设为numeric,默认值为(0),长度18,小数位为2.
    可是不行啊!
      

  7.   

    declare @length intselect @length=count(*) from b where bm='aaaa'if @length != 0
         update a set gz=(select sum(gz) from b where bm='aaaa') where id=5
    else 
         update a set gz=0 where id = 5
      

  8.   

    update 表达式A set gz=(select sum(gz) from 表B where BM='aaaa') where id=5
      

  9.   

    select sum(gz) as total from 表B where BM='aaaa'
    读取记录集中total字段的值
    if total>0
    {
       update 表达式A set gz=(select sum(gz) from 表B where BM='aaaa') where id=5
    }
    else
    {
      update 表A set gz=0 where id = 5}