0

我有这样的查询:

SELECT DATE(Inspection_datetime) AS Date,Line,TRIM(Model) AS Model, Lot_no,
    COUNT(Serial_number) AS Qty
FROM inspection_report 
WHERE Line LIKE 'FA 06' AND MONTH(Inspection_datetime) = MONTH(CURRENT_DATE)
GROUP BY DATE(Inspection_datetime), TRIM(Model),Line,Lot_no

结果:

   Date     Line    Model      Lot_no  Qty                 
2012-10-19  FA 06   KD-X30EED   012A    1
2012-10-21  FA 06   KD-R446UD   024A    20
2012-10-21  FA 06   KD-R446UD   026A    25
2012-10-22  FA 06   KD-R430JD   052A    20
2012-10-22  FA 06   KD-R446UD   026A    15
2012-10-22  FA 06   KD-R540JD   025A    30
2012-10-23  FA 06   KD-R540JD   025A    10
2012-10-23  FA 06   KD-R540JD   027A    2

如果我想要SUM这个数据之前的数量怎么办:

2012-10-22  FA 06   KD-R446UD   026A    15

我试过这个:

SELECT SUM(X.Qty) AS QTY
FROM (
    SELECT DATE(Inspection_datetime) AS Date,Line,TRIM(Model) AS Model, Lot_no,     COUNT(Serial_number) AS Qty
    FROM inspection_report 
    WHERE Line LIKE 'FA 06' AND MONTH(Inspection_datetime) = MONTH(CURRENT_DATE)
    GROUP BY DATE(Inspection_datetime), TRIM(Model),Line,Lot_no) AS X
WHERE ?????????? 

我想要这样的结果:

|Qty |
+----+       
|81  |

编辑

如果我选择数据停止计数,2012-10-21 FA 06 KD-R446UD 026A 25 我会得到Qty =46 如果这个问题不可能用 MySQL Query 做,PHP 怎么办?

4

1 回答 1

0

您可以使用运行总变量,如下所示:

SET @total = 0;
SELECT
    (@total := @total + X.qty) AS qty,
    X.Model
FROM (
    SELECT
        DATE(Inspection_datetime) AS Date,
        Line,
        TRIM(Model) AS Model,
        Lot_no,
        COUNT(Serial_number) AS Qty
    FROM inspection_report
    WHERE Line LIKE 'FA 06'
      AND MONTH(Inspection_datetime) = MONTH(CURRENT_DATE)
    GROUP BY 1, 3, 2, 4
) AS X
WHERE @total <= 81
于 2012-10-23T03:28:28.577 回答