尝试创建一个查询,让我获得总赢,抽奖和亏损。我有以下查询
SELECT CASE
WHEN m.home_team = '192'
AND m.home_full_time_score > m.away_full_time_score
OR m.away_team = '192'
AND m.home_full_time_score < m.away_full_time_score
THEN 1
END AS 'wins',
CASE
WHEN m.home_team = '192'
AND m.home_full_time_score = m.away_full_time_score
OR m.away_team = '192'
AND m.home_full_time_score = m.away_full_time_score
THEN 1
END AS 'draws',
CASE
WHEN m.home_team = '192'
AND m.home_full_time_score < m.away_full_time_score
OR m.away_team = '192'
AND m.home_full_time_score > m.away_full_time_score
THEN 1
END AS 'loss'
FROM exp_tm_matches m
INNER JOIN exp_tm_division_matches dm
ON (dm.match = m.id)
INNER JOIN exp_tm_divisions d
ON (dm.division = d.id)
WHERE m.info = 'PL'
AND (
m.home_team = '192'
OR m.away_team = '192'
)
AND dm.season = '16'
ORDER BY m.kick_off DESC LIMIT 5
这会产生以下内容
wins draws loses
NULL 1 NULL
1 NULL NULL
1 NULL NULL
Null 1 NULL
1 NULL NULL
我想要得到的是Wins = 3 Draws = 2 Loses = 0,如果我在case语句中添加count它忽略了5的LIMIT。
答案 0 :(得分:1)
使用条件聚合。你快到了:
SELECT SUM( (m.home_team = 192 AND m.home_full_time_score > m.away_full_time_score) OR
(m.away_team = 192 AND m.home_full_time_score < m.away_full_time_score
)
) as wins,
SUM(m.home_full_time_score = m.away_full_time_score)
) as draws,
SUM( (m.home_team = 192 AND m.home_full_time_score < m.away_full_time_score) OR
(m.away_team = 192 AND m.home_full_time_score > m.away_full_time_score
)
) as losses
FROM exp_tm_matches m INNER JOIN
exp_tm_division_matches dm
ON dm.match = m.id
WHERE m.info = 'PL' AND
192 IN (m.home_team, m.away_team) AND
dm.season = 16;
注意:
if()
或case
。draws
的逻辑不需要查看团队代码。limit
是不必要的。要获取最后五场比赛,请将from
和where
条款更改为:
FROM (SELECT m.*
FROM exp_tm_matches m INNER JOIN
exp_tm_division_matches dm
ON dm.match = m.id
WHERE m.info = 'PL' AND
192 IN (m.home_team, m.away_team) AND
dm.season = 16
ORDER BY m.kick_off DESC
LIMIT 5
) m
答案 1 :(得分:0)
您还需要在AND OR AND语句中添加一些括号,因为它不会以这种方式工作。
WHERE m.home_team = '192'
AND m.home_full_time_score < m.away_full_time_score
OR m.away_team = '192'
AND m.home_full_time_score > m.away_full_time_score
结果:主场或客场球队得分。
可能效果更好:
WHERE (m.home_team = '192'
AND m.home_full_time_score < m.away_full_time_score)
OR (m.away_team = '192'
AND m.home_full_time_score > m.away_full_time_score)
结果如下:主队得分<客场OR(主场比赛时)客队有得分&gt;远
答案 2 :(得分:0)
可能的解决方案如下:
SELECT SUM(wins) as total_wins,
SUM(loss) as total_losses,
SUM(draws) as total_draws
FROM
(
...add your complete query here, but you ahve to add a column we can group the results by
i.e.: SELECT m.home_team AS team
) temp
GROUP BY team
希望有所帮助
答案 3 :(得分:0)
我不得不做类似的事情。我不是一个沉重的开发类型的人,但这对我很有用。我根据我的旧查询修改了查询的第一位。它适用于我创建的样本数据
SELECT 'wins' = sum(CASE
WHEN m.home_team = '192'
AND m.home_full_time_score > m.away_full_time_score
OR m.away_team = '192'
AND m.home_full_time_score < m.away_full_time_score
THEN 1 else 0 end)
,
'draws' = sum(CASE
WHEN m.home_team = '192'
AND m.home_full_time_score = m.away_full_time_score
OR m.away_team = '192'
AND m.home_full_time_score = m.away_full_time_score
THEN 1 else 0 end)
,
'loss' = sum(CASE
WHEN m.home_team = '192'
AND m.home_full_time_score < m.away_full_time_score
OR m.away_team = '192'
AND m.home_full_time_score > m.away_full_time_score
THEN 1 else 0 end)