嗨,在更新我需要的一条记录时,如果我改变一行值,它会自动更新同一行的所有值

时间:2017-03-17 17:36:08

标签: php mysql sql

所以我只需要一个小指导如何使这个代码工作,即如果我更新任何一行一个特定的字段值然后基于所有相应的字段应该改变它的值。

如果您可以查看我附加的图像文件,我们将会清楚地了解它。清晰的场景将出现在前面。

任何建议都会对我有所帮助。

提前致谢!!

How it should work.

1 个答案:

答案 0 :(得分:0)

我认为这是你想要的那种事情

初​​始化:

declare @achieved as int
set @achieved = 55
create table #mytable (id int, total_work int, today_progress int)
insert into #mytable values
(6, 100, 0), (5, 100, 5), (4, 100, 2), (3, 100, 5), (2, 100, 3), (1, 100, 5)

检查初始值:

select M1.total_work,
       CASE
          when id = 1 then @achieved
       else   
          @achieved + (select sum(M2.today_progress) from #mytable M2 where M1.id > M2.id)
       end as 'achieved_till_date',
       M1.today_progress
from #mytable M1
order by id desc

将更新更新为today_progress

update #mytable
set today_progress = 10 where id = 2

检查最终值

select M1.total_work,
       CASE
          when id = 1 then @achieved
       else   
          @achieved + (select sum(M2.today_progress) from #mytable M2 where M1.id > M2.id)
       end as 'achieved_till_date',
       M1.today_progress
from #mytable M1
order by id desc