我有2个表  客户表(客户id,总消费金额),消费表(消费单id,客户id,单次消费金额) 一个客户对应多张消费表,现在我要把一个客户所对应的所有消费表中的消费金额总和 放在 客户表的总消费金额字段?哪个大哥帮忙写写哇

解决方案 »

  1.   

    update 客户表 set 总消费金额=(select sum(单次消费金额) from 消费表 where 客户id=客户表.客户id)
      

  2.   

    update 客户表 a
     set  总消费金额 =sum(b.单次消费金额) 
    from  消费表 b
    where a.客户id =b.客户id
      

  3.   


    update 客户表 set 总消费金额=(select sum(单次消费金额)  from (
    select 单次消费金额
    from 消费表1 where 客户id=客户表.客户id
    union all
    select 单次消费金额
    from 消费表2 where 客户id=客户表.客户id
    ) T 
    )
    不知楼主是不是这个意思,消费表是不是按时间生成的
      

  4.   

    update 客户表 
    set 总消费金额=(select sum(单次消费金额) from 消费表 group by 消费表.客户id 
    where 客户id=客户表.客户id )