gpt4 book ai didi

MySQL Volley 排名

转载 作者:可可西里 更新时间:2023-11-01 07:03:05 27 4
gpt4 key购买 nike

我有一个按游戏结果填满游戏的数据库表,想知道我是否可以计算以下内容:

  • GP(玩过的游戏)
  • 获胜
  • 失败
  • 积分(每胜2分,每负1分)

这是我的表结构:

CREATE TABLE `results` (
`id` int(10) unsigned NOT NULL auto_increment,
`home_team_id` int(10) unsigned NOT NULL,
`home_score` int(3) unsigned NOT NULL,
`visit_team_id` int(10) unsigned NOT NULL,
`visit_score` int(3) unsigned NOT NULL,
`tcl_id` int(3) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

以及一些测试结果:

INSERT INTO `results` VALUES(1, 1, 21, 2, 25, 68);
INSERT INTO `results` VALUES(2, 3, 21, 4, 17, 68);
INSERT INTO `results` VALUES(3, 1, 25, 3, 9, 68);
INSERT INTO `results` VALUES(4, 2, 7, 4, 22, 68);
INSERT INTO `results` VALUES(5, 1, 19, 4, 20, 68);
INSERT INTO `results` VALUES(6, 2, 24, 3, 26, 68);

决赛 table 看起来像这样(结果并不完全准确):

+-------------------+----+------+-------+--------+
| Team Name | GP | Wins | Loses | Points |
+-------------------+----+------+-------+--------+
| Spikers | 4 | 4 | 0 | 8 |
| Leapers | 4 | 2 | 2 | 6 |
| Ground Control | 4 | 1 | 3 | 5 |
| Touch Guys | 4 | 0 | 4 | 4 |
+-------------------+----+------+-------+--------+

需要为

添加 WHERE 子句
tcl_id
像这样:

WHERE results.tcl_id = 68

提前谢谢你。

最佳答案

这应该做你想做的:

SELECT
team_id,
COUNT(*) AS GP,
SUM(is_win) AS Wins,
SUM(NOT is_win) AS Losses,
2 * SUM(is_win) + SUM(NOT is_win) AS Points
FROM
(
SELECT
home_team_id AS team_id,
home_score > visit_score AS is_win
FROM results
WHERE tcl_id = 68
UNION ALL
SELECT
visit_team_id AS team_id,
home_score < visit_score AS is_win
FROM results
WHERE tcl_id = 68
) T1
GROUP BY team_id
ORDER BY Points DESC

示例数据的输出:

4, 3, 2, 1, 5
3, 3, 2, 1, 5
1, 3, 1, 2, 4
2, 3, 1, 2, 4

注意事项:

  • 您的示例数据似乎与您的预期输出不匹配 - 您的测试数据只有 6 场比赛,但您的预期输出有 8 场比赛。这就是为什么我的输出与您的不同。
  • 您没有提供用于从团队 ID 中获取团队名称的表格。只需加入带有团队名称的表格即可获得所需格式的结果。

关于MySQL Volley 排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2893206/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com