MySQL,过滤但计算不同的值

时间:2012-02-14 18:45:31

标签: mysql filter count

我正在写一个简单的足球数据库,在那个数据库中,我希望球队在欧足联进行比赛,但要计算欧足联的胜利数。

球队可以打欧足联,结果应该是'赢''平局'或'输'

为此,我写了这个查询,但我不知道如何更改count语句,因为此查询给出了在UEFA中玩的每个团队的匹配数。

SELECT t.name, count(*) as Wins
FROM Teams t, Matches m
WHERE competion = 'UEFA' AND t.name = m.team
GROUP BY t.name

1 个答案:

答案 0 :(得分:1)

使用适当的标准,它看起来像:

SELECT t.name, count(*) as matches,
sum(case when result = "win" then 1 else 0 end) as wins,
sum(case when result = "draw" then 1 else 0 end) as draws,
sum(case when result = "loss" then 1 else 0 end) as losses
FROM Teams t, Matches m
WHERE competion = 'UEFA' AND t.name = m.team
GROUP BY t.name