我有這樣的查詢運行得很好:MYSQL GROUP BY和WHERE子句
SELECT offers.*, COALESCE(SUM(commission),0) AS revenue, COUNT(commission) AS leads
FROM offers
LEFT JOIN offers_revenue AS or ON or.offerid=offers.offerid
GROUP BY offers.offerid
這將返回我的所有與正確comission報價表中的行相加。
現在我需要添加一個WHERE子句(可以說,當某個條件滿足並不總和):
SELECT offers.*, COALESCE(SUM(commission),0) AS revenue, COUNT(commission) AS leads, reversed
FROM offers
LEFT JOIN offers_revenue AS or ON or.offerid=offers.offerid
WHERE reversed=0
GROUP BY offers.offerid, reversed
開始我嘗試只需添加WHERE子句,但是這回我一個空集的結果,挖掘後我知道,你需要包括你的GROUP BY參數,並將它們包括在SELECT中,但仍然沒有結果。
我在這裏做錯了什麼?在第一個查詢中,我測試表上的所有結果都被設置爲reverse = 0,所以我必須在這裏丟失一些東西。
'WHERE逆轉= 0' ==>'具有相反= 0'編輯:組後添加BY .. .... –
HAVING沒有做的伎倆。 –