douaipi3965 2016-07-19 05:35
浏览 37
已采纳

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

I have the following table where parent_id, price, quantity and exp_date are primary fields.

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

I want to subtract a distinct value from the overall quantity starting from the last modified row. Right now I have this query:

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

This particular one is working because the value I subtract is more than the last row. It will output this:

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

However if I want to subtract a smaller amount like 11 for example the result will be like this:

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

instead of this:

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

What am I missing here? Thank you in advance!

  • 写回答

1条回答 默认 最新

  • doushouhe7072 2016-07-26 09:23
    关注

    Alright, so if anyone stumble upon this question in the future the following queries worked perfectly:

    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;
    

    The above works as following:

    1. add a new row to the table called helper
    2. set the value of quantity and @remain (latter is necessary because if it is negative we want to continue the subtraction)
    3. Overwrite the value of @remain based on its sign
    4. Drop the helper row
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度