Return 0 如果条件不匹配
Return 0 if the condition doesn't match
我有一款足球比赛,人们可以在其中预测比赛。如果他们认为主队获胜,他们可以给一场比赛打 1 分,如果客队赢球则给 2 分,如果平局则给 3 分。如果他们正确预测比赛,他们就会得到一分。我的数据库中的表如下所示:
Table 匹配
| id | home | away | result | round_id
| --- | ---------| -------- -| -------|----------|
| 1 | id club 1| id club 2 | 1 | id round 1
| 2 | id club 5| id club 4 | 3 | id round 1
| 3 | id club 8| id club 5 | 1 | id round 2
Table predictions
| prediction | user_id | match_id |
| -------- | -------------- | -------- |
| 1 | id user 1 | id match 1
| 3 | id user 1 | id match 2
| 2 | id user 1 | id match 3
最初我想在 PHP 中计算分数,但我认为这也应该仅通过 MySQL 即可。所以我尝试了一些东西并提出了以下查询:
SELECT Count(*) AS points,
username,
round_id
FROM predictions
LEFT JOIN matches
ON predictions.match_id = matches.id
INNER JOIN users
ON predictions.user_id = users.id
WHERE predictions.prediction = matches.result
GROUP BY username,
round_id
ORDER BY points DESC,
username ASC
查询正确计算了每个用户每轮的分数,唯一的问题是如果参与者在游戏轮中没有任何权利,它根本不会出现在列表中。如果他们有 0 分,是否有人知道如何做才能让参与者进入列表?转换为上面提到的表格,第 2 轮不会 return 查询,因为其中唯一的匹配被错误预测。然而,我确实想要这个,所以第 2 轮 returned 的分数为 0。
我想要的结果:
| points | username | round_id|
| -------- | -------------- | --------|
| 2 | John | 1
| 0 | John | 2
我现在的结果:
| points | username | round_id|
| -------- | -------------- | --------|
| 2 | John | 1
WHERE
子句:
WHERE predictions.prediction = matches.result
过滤掉任何错误的预测,但即使您删除它,聚合函数 COUNT(*)
也会统计错误的预测。
像这样加入和分组:
SELECT SUM(p.prediction = m.result) AS points,
u.username,
m.round_id
FROM users u
INNER JOIN predictions p ON p.user_id = u.id
INNER JOIN matches m ON m.id = p.match_id
GROUP BY u.id, u.username, m.round_id
ORDER BY points DESC, u.username ASC;
聚合函数 SUM()
将对布尔表达式 prediction = result
求和,true
的计算结果为 1
,false
的计算结果为 0
.
参见demo。
我有一款足球比赛,人们可以在其中预测比赛。如果他们认为主队获胜,他们可以给一场比赛打 1 分,如果客队赢球则给 2 分,如果平局则给 3 分。如果他们正确预测比赛,他们就会得到一分。我的数据库中的表如下所示:
Table 匹配
| id | home | away | result | round_id
| --- | ---------| -------- -| -------|----------|
| 1 | id club 1| id club 2 | 1 | id round 1
| 2 | id club 5| id club 4 | 3 | id round 1
| 3 | id club 8| id club 5 | 1 | id round 2
Table predictions
| prediction | user_id | match_id |
| -------- | -------------- | -------- |
| 1 | id user 1 | id match 1
| 3 | id user 1 | id match 2
| 2 | id user 1 | id match 3
最初我想在 PHP 中计算分数,但我认为这也应该仅通过 MySQL 即可。所以我尝试了一些东西并提出了以下查询:
SELECT Count(*) AS points,
username,
round_id
FROM predictions
LEFT JOIN matches
ON predictions.match_id = matches.id
INNER JOIN users
ON predictions.user_id = users.id
WHERE predictions.prediction = matches.result
GROUP BY username,
round_id
ORDER BY points DESC,
username ASC
查询正确计算了每个用户每轮的分数,唯一的问题是如果参与者在游戏轮中没有任何权利,它根本不会出现在列表中。如果他们有 0 分,是否有人知道如何做才能让参与者进入列表?转换为上面提到的表格,第 2 轮不会 return 查询,因为其中唯一的匹配被错误预测。然而,我确实想要这个,所以第 2 轮 returned 的分数为 0。
我想要的结果:
| points | username | round_id|
| -------- | -------------- | --------|
| 2 | John | 1
| 0 | John | 2
我现在的结果:
| points | username | round_id|
| -------- | -------------- | --------|
| 2 | John | 1
WHERE
子句:
WHERE predictions.prediction = matches.result
过滤掉任何错误的预测,但即使您删除它,聚合函数 COUNT(*)
也会统计错误的预测。
像这样加入和分组:
SELECT SUM(p.prediction = m.result) AS points,
u.username,
m.round_id
FROM users u
INNER JOIN predictions p ON p.user_id = u.id
INNER JOIN matches m ON m.id = p.match_id
GROUP BY u.id, u.username, m.round_id
ORDER BY points DESC, u.username ASC;
聚合函数 SUM()
将对布尔表达式 prediction = result
求和,true
的计算结果为 1
,false
的计算结果为 0
.
参见demo。