我正在查詢一張名爲Events
的表,該錶針對黃卡和紅卡(它是足球應用程序)預訂量最多的玩家。黃色是值得1點和紅色是值得3.在具有多個計數的查詢中一起添加兩個引用
我有這個疑問:
SELECT player_id, first_name, last_name, slug,
SUM(CASE WHEN event_type.name = 'Yellow Card' THEN 1 ELSE 0 END) AS yellowAmount,
SUM(CASE WHEN event_type.name = 'Red Card' THEN 3 ELSE 0 END) AS redAmount
FROM event
INNER JOIN matches ON event.match_id = matches.id
INNER JOIN player ON event.player_id = player.id
INNER JOIN event_type ON event.event_type_id = event_type.id
WHERE matches.league_id = 94
AND (event_type.name = "Yellow Card" OR event_type.name = "Red Card")
GROUP BY player_id;
你如何添加yellowAmount
和redAmount
在一起,並使用該命令的結果?
我已經試過,但我得到一個未知的列錯誤:
SELECT player_id, first_name, last_name, slug,
SUM(CASE WHEN event_type.name = 'Yellow Card' THEN 1 ELSE 0 END) AS yellowAmount,
SUM(CASE WHEN event_type.name = 'Red Card' THEN 3 ELSE 0 END) AS redAmount,
SUM(yellowAmount + redAmount) AS total
FROM event
INNER JOIN matches ON event.match_id = matches.id
INNER join player ON event.player_id = player.id
INNER JOIN event_type ON event.event_type_id = event_type.id
WHERE matches.league_id = 94
AND (event_type.name = "Yellow Card" OR event_type.name = "Red Card")
GROUP BY player_id
ORDER BY total DESC;
sum(case when event_type.name ='Yellow Card'then 1 else 0 end)+ sum(case when event_type.name ='Red Card'then 3 else 0 end) – Strawberry