首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

请问一个SQL

2012-01-10 
请教一个SQL有两个表A:ID,GZB:BM,GZ我现在想要求修改A表中ID为5的记录的GZ字段,使其等于B表中所有BM AAAA

请教一个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

热点排行