你錯過此列後一個逗號:
`p`.`name`
考慮這個查詢,而不是:
SELECT i.itemtype AS Item
, p.name
, SUM(CASE
WHEN itemtype = 2148 THEN i.count
ELSE 0
END) AS Count
, SUM(CASE
WHEN itemtype = 2152 THEN i.count
ELSE 0
END) * 100 AS Count1
, SUM(CASE
WHEN itemtype = 2160 THEN i.count
ELSE 0
END) * 10000 AS Count2
FROM player_items AS i
LEFT JOIN players AS p ON i.player_id = p.id
WHERE i.itemtype IN (2148, 2152, 2160)
GROUP BY i.itemtype ASC
LIMIT 0, 30
總結這三列在一起,你可以:
SELECT t.itemType AS Item, t.Count, t.Count1, t.Count2
, SUM(t.Count1, t.Count2, t.Count3) AS Total
FROM
(
SELECT i.itemtype AS Item
, p.name
, SUM(CASE
WHEN itemtype = 2148 THEN i.count
ELSE 0
END) AS Count
, SUM(CASE
WHEN itemtype = 2152 THEN i.count
ELSE 0
END) * 100 AS Count1
, SUM(CASE
WHEN itemtype = 2160 THEN i.count
ELSE 0
END) * 10000 AS Count2
FROM player_items AS i
LEFT JOIN players AS p ON i.player_id = p.id
WHERE i.itemtype IN (2148, 2152, 2160)
GROUP BY i.itemtype ASC
LIMIT 0, 30
) AS t
或者你也可以添加其他CASE
SELECT i.itemtype AS Item
, p.name
, SUM(CASE
WHEN itemtype = 2148 THEN i.count
ELSE 0
END) AS Count
, SUM(CASE
WHEN itemtype = 2152 THEN i.count
ELSE 0
END) * 100 AS Count1
, SUM(CASE
WHEN itemtype = 2160 THEN i.count
ELSE 0
END) * 10000 AS Count2
, SUM(CASE
WHEN itemtype = 2148 THEN i.count
WHEN itemtype = 2152 THEN (i.count * 100)
WHEN itemtype = 2160 THEN (i.count * 10000)
ELSE 0
END) AS Total
FROM player_items AS i
LEFT JOIN players AS p ON i.player_id = p.id
WHERE i.itemtype IN (2148, 2152, 2160)
GROUP BY i.itemtype ASC
LIMIT 0, 30
的CASE statements,便無需您必須運行三個額外的SELECT
語句才能返回相同的結果。
感謝您的好解決方案。順便說一句。是否有可能在另一行中將'Count','Count1'和'Count2'加在一起? – Cyclone
@Cyclone我已經更新了我的答案,以解決您評論中的問題。 –
謝謝:-)))。 – Cyclone