我有桌子:
+-------------+------------+---------+
| Code | Num | Total |
+-------------+------------+---------+
| A | 5 | 10 |
+-------------+------------+---------+
| B | 7 | 17 |
+-------------+------------+---------+
| C | 8 | 25 |
+-------------+------------+---------+
现在,如果我想将记录B的Num值从7改为9而不是记录B的总值 是9和记录C 27.如何编写将从特定记录开始更改所有总和的查询?
答案 0 :(得分:0)
工作小提琴:http://sqlfiddle.com/#!2/41cb1/1/0
create table b as
(Select 'A' as code,5 as num ,10 as total)
UNION
(Select 'B' as code,7 as num ,17 as total)
UNION
(Select 'C' as code,8 as num ,25 as total);
update b set num = 9 where code = 'B';
set @var=5;
Update b
INNER JOIN
(Select code, num, b.total, @var:=@var+num as NewTotal from b order by code) c
on c.Code=B.code
set b.total=NewTotal;
select * from b;