我有一個查詢如下:如何在查詢中進行數學運算?
SELECT u.username, u.id, COUNT(t.tahmin) AS tahmins_no,
SUM(t.result = 1) AS winnings,
SUM(t.result = 2) AS loses,
(SUM(t.tahmin = 1)*1) AS ms1,
(SUM(t.tahmin = 2)*3) AS ms0,
(SUM(t.tahmin = 3)*1) AS ms2
FROM users u
LEFT JOIN tahminler t ON u.id = t.user_id
GROUP BY u.id
HAVING tahmins_no > 0
我想讓(SUM(t.tahmin = 1)*1) AS ms1
只有result = 1
(如果結果爲1個加總結否則不加),結果是在相同的記錄中的值來計算。這有可能沒有進行子查詢?我不希望有複雜的未優化的查詢。
你沒有得到你所期望的結果?您使用的語法在MySQL中有效。 –