读书人

请问一个SQL

发布时间: 2012-01-10 21:26:50 作者: rapoo

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

[解决办法]
有两个表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
[解决办法]
declare @length int

select @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

读书人网 >asp.net

热点推荐