php - MySQL 在一个查询中从多行中减去

标签 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             |
+-----------+-------+----------+------------+---------------------+------------------+

我在这里缺少什么? 预先感谢您!

最佳答案

好吧,如果将来有人偶然发现这个问题,以下查询可以完美地工作:

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. 设置数量和@remain的值(后者是必要的,因为如果它是负数,我们要继续减法)
  3. 根据符号覆盖@remain的值
  4. 删除辅助行

关于php - MySQL 在一个查询中从多行中减去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450274/

相关文章:

mysql - 共同的 friend -MySQL

mysql - 识别多行插入的哪个值不符合外键约束

php - 将 .htaccess 重定向到 ssl 不起作用

php - 获取多个过滤值参数

php - 登录 cookie 不在页面刷新时保存

mysql - Nginx Php5-fpm 配置在 Maverisks 上不起作用

python - 为什么在建立 mysql 到 python 连接时出错?

php - 如何使用php向mysql插入多维数组字段和数组行

javascript - 从 javascript/jquery 中的 formData 中删除特定文件

php - Laravel 查询获取不同值的出现次数