2011-03-05 233 views
2

以下是需要修改的查詢。我已附加了屏幕的輸出。在這裏,我只需要結果maxBuy小於或等於totalDealsBought。我應該如何更改此查詢的O/P。mysql查詢需要修改

編輯:一個更多的小改變在這個查詢完成。 這是我需要顯示以下格式的結果:

1.凡totalDealsBought應小於maxBuy應顯示第一。

這怎麼辦?

SELECT d.id, d.dealTitle, d.expiryDate, d.dealMainImage, d.actualPrice, d.discount, d.offerValue, d.maxBuy, sum(sc.quantity) AS totalDealsBought 
    FROM deal AS d 
    LEFT JOIN shoppingcart AS sc ON sc.dealID = d.id 
    WHERE CURDATE() != d.startDate 
    AND d.startDate < CURDATE() 
    AND d.status = 'Active' 
    AND d.cities = 'chennai' 
    AND sc.paymentStatus = 'paid' 
    GROUP BY d.id 

enter image description here

在此先感謝。

回答

4

你有沒有試着用

GROUP BY d.id 
HAVING d.maxBuy <= sum(sc.quantity) 
+1

好,但他已經化名SUM(sc.quantity)到totalDealsBought。 – 2011-03-05 07:03:26

6

將以下行 HAVING maxBuy <= totalDealsBought 添加到查詢的末尾。