Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从上一行中减去数量_Mysql - Fatal编程技术网

Mysql 从上一行中减去数量

Mysql 从上一行中减去数量,mysql,Mysql,这件事很棘手。我用PHP回传这些数据,但我认为最好用MySQL进行查询 这是我的桌子: 我的问题(错): 仅检索两行,并且销售数量错误 我试图实现的结果如下所示: Quantity | timestamp | sold_qty 985 2016-12-27 0 960 2016-12-28 25 955 2016-12-29 5 有人能帮忙吗?一个选项是使用相关子查询。对于大型集合,这当然不是最有效的方

这件事很棘手。我用PHP回传这些数据,但我认为最好用MySQL进行查询

这是我的桌子:

我的问题(错):

仅检索两行,并且销售数量错误

我试图实现的结果如下所示:

Quantity |  timestamp   | sold_qty
985         2016-12-27      0  
960         2016-12-28      25
955         2016-12-29      5

有人能帮忙吗?

一个选项是使用相关子查询。对于大型集合,这当然不是最有效的方法,但是对于返回的行数有限的情况,这是可行的

似乎有一个您希望处理和返回行的序列,但没有ORDER BY子句。从示例数据来看,它看起来要么按
时间戳
列升序,要么按
数量
列降序,要么按
id
升序。我们只是猜测

假设序列是通过
时间戳
id
,并假设
(时间戳,id)
元组是唯一的。。。这些都是相当合理的假设,但它们只是假设,而且是相当大的假设

SELECT ih.Quantity
     , ih.timestamp

     , IFNULL(
                 ( SELECT pr.Quantity
                     FROM InventoryTest_history pr
                    WHERE pr.SKU = ih.SKU
                      AND pr.timestamp <= ih.timestamp
                      AND (pr.timestamp < ih.timestamp OR pr.id < ih.id )
                    ORDER BY pr.SKU DESC, pr.timestamp DESC, pr.id DESC
                    LIMIT 1
                 )
                 - ih.Quantity
       ,0) AS sold_qty

  FROM InventoryTest_history ih
 WHERE ih.SKU = '(L) U-Joint'
 ORDER BY ... 
如果认为行的顺序不是按
(timestamp,id)
,则需要修改子查询的
顺序和
WHERE
子句中的条件



这只是一种可能的方法。还有其他查询模式将返回相同的结果。

一个选项是使用相关子查询。对于大型集合,这当然不是最有效的方法,但是对于返回的行数有限的情况,这是可行的

似乎有一个您希望处理和返回行的序列,但没有
ORDER BY子句。从示例数据来看,它看起来要么按
时间戳
列升序,要么按
数量
列降序,要么按
id
升序。我们只是猜测

假设序列是通过
时间戳
id
,并假设
(时间戳,id)
元组是唯一的。。。这些都是相当合理的假设,但它们只是假设,而且是相当大的假设

SELECT ih.Quantity
     , ih.timestamp

     , IFNULL(
                 ( SELECT pr.Quantity
                     FROM InventoryTest_history pr
                    WHERE pr.SKU = ih.SKU
                      AND pr.timestamp <= ih.timestamp
                      AND (pr.timestamp < ih.timestamp OR pr.id < ih.id )
                    ORDER BY pr.SKU DESC, pr.timestamp DESC, pr.id DESC
                    LIMIT 1
                 )
                 - ih.Quantity
       ,0) AS sold_qty

  FROM InventoryTest_history ih
 WHERE ih.SKU = '(L) U-Joint'
 ORDER BY ... 
如果认为行的顺序不是按
(timestamp,id)
,则需要修改子查询的
顺序和
WHERE
子句中的条件



这只是一种可能的方法。还有其他查询模式将返回相同的结果。

您也可以在不联接表的情况下使用此模式:out SELECT仅用于隐藏某些列

SELECT Quantity, `timestamp`, sold_qty
FROM (
    SELECT i.*,
    @sold_qty := GREATEST(@last_qty - i.`Quantity`,0) as sold_qty,
    @last_qty := i.`Quantity` as last_qty
    FROM InventoryTest_history i
    CROSS JOIN ( SELECT @last_qty := '', @sold_qty := 0) as init
    ORDER BY `timestamp`
    ) as result;
样本

mysql> SELECT * from InventoryTest_history;

+----+-------------+----------+---------------------+
| id | SKU         | Quantity | timestamp           |
+----+-------------+----------+---------------------+
|  1 | (L) U-Joint |      985 | 2016-12-27 10:08:58 |
|  2 | (L) U-Joint |      960 | 2016-12-28 10:09:52 |
|  3 | (L) U-Joint |      955 | 2016-12-29 16:01:02 |
+----+-------------+----------+---------------------+
3 rows in set (0,02 sec)

mysql> SELECT Quantity, `timestamp`, sold_qty
    -> FROM (
    ->     SELECT i.*,
    ->     @sold_qty := GREATEST(@last_qty - i.`Quantity`,0) as sold_qty,
    ->     @last_qty := i.`Quantity` as last_qty
    ->     FROM InventoryTest_history i
    ->     CROSS JOIN ( SELECT @last_qty := '', @sold_qty := 0) as init
    ->     ORDER BY `timestamp`
    ->     ) as result;
+----------+---------------------+----------+
| Quantity | timestamp           | sold_qty |
+----------+---------------------+----------+
|      985 | 2016-12-27 10:08:58 |        0 |
|      960 | 2016-12-28 10:09:52 |       25 |
|      955 | 2016-12-29 16:01:02 |        5 |
+----------+---------------------+----------+
3 rows in set (0,00 sec)

mysql> SELECT i.*,
    -> @sold_qty := GREATEST(@last_qty - i.`Quantity`,0) as sold_qty,
    -> @last_qty := i.`Quantity` as last_qty
    -> FROM InventoryTest_history i
    -> CROSS JOIN ( SELECT @last_qty := '', @sold_qty := 0) as init
    -> ORDER BY `timestamp`;
+----+-------------+----------+---------------------+----------+----------+
| id | SKU         | Quantity | timestamp           | sold_qty | last_qty |
+----+-------------+----------+---------------------+----------+----------+
|  1 | (L) U-Joint |      985 | 2016-12-27 10:08:58 |        0 |      985 |
|  2 | (L) U-Joint |      960 | 2016-12-28 10:09:52 |       25 |      960 |
|  3 | (L) U-Joint |      955 | 2016-12-29 16:01:02 |        5 |      955 |
+----+-------------+----------+---------------------+----------+----------+
3 rows in set (0,00 sec)

mysql>

您也可以在不联接表的情况下使用此选项:out SELECT仅用于隐藏某些列

SELECT Quantity, `timestamp`, sold_qty
FROM (
    SELECT i.*,
    @sold_qty := GREATEST(@last_qty - i.`Quantity`,0) as sold_qty,
    @last_qty := i.`Quantity` as last_qty
    FROM InventoryTest_history i
    CROSS JOIN ( SELECT @last_qty := '', @sold_qty := 0) as init
    ORDER BY `timestamp`
    ) as result;
样本

mysql> SELECT * from InventoryTest_history;

+----+-------------+----------+---------------------+
| id | SKU         | Quantity | timestamp           |
+----+-------------+----------+---------------------+
|  1 | (L) U-Joint |      985 | 2016-12-27 10:08:58 |
|  2 | (L) U-Joint |      960 | 2016-12-28 10:09:52 |
|  3 | (L) U-Joint |      955 | 2016-12-29 16:01:02 |
+----+-------------+----------+---------------------+
3 rows in set (0,02 sec)

mysql> SELECT Quantity, `timestamp`, sold_qty
    -> FROM (
    ->     SELECT i.*,
    ->     @sold_qty := GREATEST(@last_qty - i.`Quantity`,0) as sold_qty,
    ->     @last_qty := i.`Quantity` as last_qty
    ->     FROM InventoryTest_history i
    ->     CROSS JOIN ( SELECT @last_qty := '', @sold_qty := 0) as init
    ->     ORDER BY `timestamp`
    ->     ) as result;
+----------+---------------------+----------+
| Quantity | timestamp           | sold_qty |
+----------+---------------------+----------+
|      985 | 2016-12-27 10:08:58 |        0 |
|      960 | 2016-12-28 10:09:52 |       25 |
|      955 | 2016-12-29 16:01:02 |        5 |
+----------+---------------------+----------+
3 rows in set (0,00 sec)

mysql> SELECT i.*,
    -> @sold_qty := GREATEST(@last_qty - i.`Quantity`,0) as sold_qty,
    -> @last_qty := i.`Quantity` as last_qty
    -> FROM InventoryTest_history i
    -> CROSS JOIN ( SELECT @last_qty := '', @sold_qty := 0) as init
    -> ORDER BY `timestamp`;
+----+-------------+----------+---------------------+----------+----------+
| id | SKU         | Quantity | timestamp           | sold_qty | last_qty |
+----+-------------+----------+---------------------+----------+----------+
|  1 | (L) U-Joint |      985 | 2016-12-27 10:08:58 |        0 |      985 |
|  2 | (L) U-Joint |      960 | 2016-12-28 10:09:52 |       25 |      960 |
|  3 | (L) U-Joint |      955 | 2016-12-29 16:01:02 |        5 |      955 |
+----+-------------+----------+---------------------+----------+----------+
3 rows in set (0,00 sec)

mysql>

你的假设是正确的,谢谢你的信息!有趣的方法。唯一的问题是它返回的是负值,
0
-25
-5
,但在其他方面效果很好:)@bbruman:Doh!我的错。负值是因为表达式正在向后执行减法。。。执行
b-a
而不是
a-b
。。。我已经编辑了答案来解决这个问题。你的假设是正确的,谢谢你提供的信息!有趣的方法。唯一的问题是它返回的是负值,
0
-25
-5
,但在其他方面效果很好:)@bbruman:Doh!我的错。负值是因为表达式正在向后执行减法。。。执行
b-a
而不是
a-b
。。。我编辑了答案来解决这个问题。非常感谢代码。我正在努力完成每一件作品,这样我才能完全理解它。在我的表上做一些测试,我相信
best()
函数会有问题。我的意思是:
最后一行的售出数量
必须是
-7
,以反映新的进货和退货。我想知道您对修改此代码以支持负数的想法。非常感谢代码。我正在努力完成每一件作品,这样我才能完全理解它。在我的表上做一些测试,我相信
best()
函数会有问题。我的意思是:
最后一行的售出数量
必须是
-7
,以反映新的进货和退货。我想知道你对修改这个以支持负数的想法。