Mysql 按日期更新行顺序

Mysql 按日期更新行顺序,mysql,Mysql,我早上问了这个问题,但没有得到任何答案。所以我删除了前一个,再次问这个问题,因为我被困了很长一段时间。希望你们能帮助我 我有一个名为加班的表,如下所示: | total | remain | t_date | |-------|--------|---------------------| | 3 | 0 | 2016-01-01 12:20:00 | | 4 | 0 | 2016-02-01 13:10:00 | | 2 |

我早上问了这个问题,但没有得到任何答案。所以我删除了前一个,再次问这个问题,因为我被困了很长一段时间。希望你们能帮助我

我有一个名为加班的表,如下所示:

| total | remain |              t_date |
|-------|--------|---------------------|
|     3 |      0 | 2016-01-01 12:20:00 |
|     4 |      0 | 2016-02-01 13:10:00 |
|     2 |      0 | 2016-03-01 14:40:00 |
|     3 |      0 | 2016-04-01 10:20:00 |
|     5 |      2 | 2016-05-01 17:20:00 |
update overtime t1
join (
  select overtime.*,
  total - remain, IF(@h > (total - remain), total, @h + remain) as h,
  @h := IF(@h > (total - remain), @h - (total - remain), 0)
  from overtime
  cross join (
      select @h := 9
  ) t
  order by t_date desc
) t2 on t1.t_date = t2.t_date
set t1.remain = t2.h;
我想按t_date desc更新列reserve order,我还有一个输入参数,假设它是$h=9,预期结果是:

| total | remain |              t_date |
|-------|--------|---------------------|
|     5 |      5 | 2016-05-01 17:20:00 | -- remain will be updated to 5 cause total = 5, then $h(6) = $h(9) - (total(5) - remain(2)) 
|     3 |      3 | 2016-04-01 10:20:00 | -- remain will be updated to 3 cause total = 3, then $h(3) = $h(6) - (total(3) - remain(0)) 
|     2 |      2 | 2016-03-01 14:40:00 | -- remain will be updated to 2 cause total = 2, then $h(1) = $h(3) - (total(2) - remain(0))
|     4 |      1 | 2016-02-01 13:10:00 | -- remain will be updated to 1 cause $h only has 1, then $h will be 0
|     3 |      0 | 2016-01-01 12:20:00 | -- cause $h = 0, this row has no need to be updated
编辑: 示例数据如上图所示,我要做的是更新列remain,并且remain的值基于total和一个输入参数,假设它是9:

更新订单是按t_日期描述的订单。例如,我必须先更新2016-05-01 17:20:00行,然后更新2016-04-01 10:20:00,然后更新2016-03-01 14:40:00,依此类推。。。 参数为9,它将分配给每一行,剩余值应更新为total的值。例如,第一行2016-05-01 17:20:00,总计=5,剩余=2,所以剩余将更新为5,参数将减去总计-剩余,它将为6,并进行下一行的分配,直到2016-02-01 13:10:00,参数为1,所以此行的剩余只需更新为1。而另一行将不需要更新。 。 如果有任何不清楚的问题,请留下评论,我可以解释


感谢您的帮助。提前感谢。

您可以使用子查询,使用变量@h进行计算:


参见此

您可以使用子查询,使用变量@h进行计算:

请参见此

选择查询:

更新查询:

演示显示了在通过上述更新查询进行更新后,按日期降序排序的表数据

更多:

选择查询:

更新查询:

演示显示了在通过上述更新查询进行更新后,按日期降序排序的表数据

更多:


必须使用子查询来执行此操作,如下所示:

| total | remain |              t_date |
|-------|--------|---------------------|
|     3 |      0 | 2016-01-01 12:20:00 |
|     4 |      0 | 2016-02-01 13:10:00 |
|     2 |      0 | 2016-03-01 14:40:00 |
|     3 |      0 | 2016-04-01 10:20:00 |
|     5 |      2 | 2016-05-01 17:20:00 |
update overtime t1
join (
  select overtime.*,
  total - remain, IF(@h > (total - remain), total, @h + remain) as h,
  @h := IF(@h > (total - remain), @h - (total - remain), 0)
  from overtime
  cross join (
      select @h := 9
  ) t
  order by t_date desc
) t2 on t1.t_date = t2.t_date
set t1.remain = t2.h;

必须使用子查询来执行此操作,如下所示:

| total | remain |              t_date |
|-------|--------|---------------------|
|     3 |      0 | 2016-01-01 12:20:00 |
|     4 |      0 | 2016-02-01 13:10:00 |
|     2 |      0 | 2016-03-01 14:40:00 |
|     3 |      0 | 2016-04-01 10:20:00 |
|     5 |      2 | 2016-05-01 17:20:00 |
update overtime t1
join (
  select overtime.*,
  total - remain, IF(@h > (total - remain), total, @h + remain) as h,
  @h := IF(@h > (total - remain), @h - (total - remain), 0)
  from overtime
  cross join (
      select @h := 9
  ) t
  order by t_date desc
) t2 on t1.t_date = t2.t_date
set t1.remain = t2.h;

再次询问:reside将更新为5,因为total=5,然后$h6=$h9-total5-remain2。你能详细解释一下吗?你的问题我一点也不清楚。请澄清用于进行更新的逻辑。小提琴是空的。为什么最后一列的第三行没有1?总共有2个可用,h足够大,所以是否应该从前3个中取2个,即保留在第二行的值?@1000111检查我编辑的任务。@TimBiegeleisen检查我编辑的任务。再次询问:保留将更新为5,因为总计=5,然后$h6=$h9-总计5-保留2。你能详细解释一下吗?你的问题我一点也不清楚。请澄清用于进行更新的逻辑。小提琴是空的。为什么最后一列的第三行没有1?总共有2个可用的,h足够大,所以是否应该从前3个中取2个,即保留在第2行的值?@1000111检查我编辑的问题。@TimBiegeleisen检查我编辑的问题。谢谢。这差不多了,但是如果@h=2,我想保持为4total+@h,这将得到2。好的,我看到在我缺席的情况下,答案已经被接受了。不过我还是更新了我的公式,因为我认为它更容易阅读。谢谢。这差不多了,但是如果@h=2,我想保持为4total+@h,这将得到2。好的,我看到在我缺席的情况下,答案已经被接受了。不过我还是更新了我的公式,因为我认为它更容易阅读。谢谢。这也差不多了,但如果@h=2,我想保持为4total+@h,这将得到2。同样模糊的要求。你能解释一下原因吗?很抱歉,这更像是一个回滚操作,事实上,我已经从RESTRING获得了9,并在t_date asc之前订购,然后出于某种原因,我想将这9返回到RESTRING,所以如果我得到的是2,这意味着从2016-05-01 17:20:00开始,现在它仍然是2,所以当返回到这一行时,它们将相加。谢谢。这也差不多了,但如果@h=2,我想保持为4total+@h,这将得到2。同样模糊的要求。你能解释一下原因吗?很抱歉,这更像是一个回滚操作,事实上,我从RESTRING获得了9,并在t_date asc之前订购,然后出于某种原因,我想将9返回到RESTRING,所以如果我得到的是2,这意味着从2016-05-01 17:20:00开始,现在它仍然是2,所以当返回到这一行时,它将对它们求和。