2015-10-24 92 views
2

我有一個MySQL表,交易,與字段ID,交易類型,數量,價格等我想通過一列的累計總和限制記錄。所以我想抽出所有交易,直到累計數量達到我的變量(這裏< = 50)。限制查詢的價值總和

我在做什麼錯?

SET @qsum := 0; 
SELECT * 
FROM (
    SELECT *, (@qsum := @qsum + quantity) AS cumulative_quantity 
    FROM transactions ORDER BY id DESC 
) transactions 
WHERE 
transactionType = 'buy' 
AND typeID = 10 
AND cumulative_quantity <= 50 
+0

不要忘記給予好評,downvote,接受綠色的勾號,其以往任何時候都合適的話任何答案。這是我們對我們工作的反饋。 – Drew

回答

0

嘗試這種方式

SET @qsum := 0; 
SELECT * 
FROM (
    SELECT *, (@qsum := @qsum + quantity) AS cumulative_quantity 
    FROM transactions 
    WHERE transactionType = 'buy' 
    AND typeID = 10 
    AND cumulative_quantity <= 50 
    ORDER BY id DESC 
) transaction