MySQL从一个查询中的多行中减去

时间:2016-07-19 05:35:56

标签: php mysql

我的下表中 parent_id 价格数量 exp_date 主要字段

+-----------+-------+----------+------------+---------------------+------------------+
| parent_id | price | quantity | exp_date   | last_modified       | last_modified_by |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 410   | 3.00     | 2016-07-30 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 300   | 10.00    | 0000-00-00 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 540   | 17.00    | 2016-07-22 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+

我想从上一个修改行开始,从总数量中减去一个不同的值。 现在我有这个问题:

SET @remain = -19;
Update Stock_props SET quantity =
(SELECT IF((@remain := quantity+@remain) < 0,'0',@remain) as quantity)
WHERE parent_id = 2
ORDER BY last_modified DESC

这个特殊的工作正在起作用,因为我减去的值大于最后一行。它将输出:

+-----------+-------+----------+------------+---------------------+------------------+
| parent_id | price | quantity | exp_date   | last_modified       | last_modified_by |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 410   | 0.00     | 2016-07-30 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 300   | 0.00     | 0000-00-00 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 540   | 11.00    | 2016-07-22 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+

但是如果我想减去像11这样的较小量,例如结果将是这样的:

+-----------+-------+----------+------------+---------------------+------------------+
| parent_id | price | quantity | exp_date   | last_modified       | last_modified_by |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 410   | 2.00     | 2016-07-30 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 300   | 0.00     | 0000-00-00 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 540   | 19.00    | 2016-07-22 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+

而不是:

+-----------+-------+----------+------------+---------------------+------------------+
| parent_id | price | quantity | exp_date   | last_modified       | last_modified_by |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 410   | 0.00     | 2016-07-30 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 300   | 2.00     | 0000-00-00 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+
| 2         | 540   | 17.00    | 2016-07-22 | 2016-07-22 18:14:34 | test             |
+-----------+-------+----------+------------+---------------------+------------------+

我在这里缺少什么? 提前谢谢!

1 个答案:

答案 0 :(得分:2)

好吧,所以如果有人在将来偶然发现这个问题,那么下面的查询就会完美地运作:

ALTER TABLE Stock_props ADD helper numeric;
SET @remain = 11;
Update Stock_props SET quantity =
(SELECT IF(((@remain := quantity+@remain) < 0),0,@remain) as quantity),
helper = (SELECT IF((@remain>0), @remain:=0,@remain)as helper),
ORDER BY last_modified DESC;
ALTER TABLE Stock_props DROP helper;

以上内容如下:

  1. 在名为helper
  2. 的表中添加一个新行
  3. 设置数量和@remain的值(后者是必要的,因为如果它是负数我们想继续减法)
  4. 根据符号
  5. 覆盖@remain的值
  6. 删除辅助行