我需要计算每个玩家的总分(cn_category.category_points
)(cn_players
记录)。这由表cn_records
驱动,该表包含当前和过时的记录。我需要仅为所有类别(cn_category
记录),地图(cn_maps
记录;未在下面显示)和游戏(cn_wads
记录;未在下面显示)的最佳时间求和。此外,对于类别#9,#10和#11,将为三个最佳分数/记录奖励积分,而不仅仅是最好的。
到目前为止,我有这个查询,但它同时计算当前和过时的记录:
SELECT *, MIN(r.record_time), SUM(cn_category.category_points)
FROM (select * from cn_records as r order by r.record_time ASC) AS r
LEFT JOIN cn_players ON r.player1_id = cn_players.player_id
LEFT JOIN cn_category ON r.category_id = cn_category.category_id
WHERE r.category_id BETWEEN 1 AND 25
GROUP BY r.player1_id
ORDER BY SUM(category_points), r.player1_id DESC
(注意:以上不是完整的查询。为简单起见,删除了一些连接和WHERE
- 子句条件。)
类别位于cn_category
表中:
category_id | category_points | ...
-------------+-----------------+-----
1 | 1.0 | ...
9 | 5.0 | ...
... | ... | ...
玩家在cn_players
表中:
player_id | ...
-----------+-----
1 | ...
2 | ...
... | ...
“记录”位于cn_records
表格中(有数千行):
record_id | category_id | player1_id | record_time | ...
-----------+-------------+------------+-------------+-----
1 | 1 | 143 | 00:00:00 | ...
2 | 1 | 145 | 00:00:00 | ...
3 | 1 | 41 | 00:00:00 | ...
4 | 1 | 41 | 00:00:00 | ...
5 | 1 | 141 | 00:00:00 | ...
6 | 1 | 41 | 00:00:00 | ...
... | ... | ... | ... | ...
答案 0 :(得分:0)
这样的东西?首先从cn_records表中选择每个类别的最大点数。然后,您可以再次加入cn_records表,仅选择每个类别的实际记录(与最大点匹配的记录)。
之后,您可以加入播放器和类别详细信息,并根据需要进行其他分组和过滤。困难的部分已经完成。 :)
SELECT
p.player_id,
p.player_name,
sum(c.category_points) AS total_points
FROM
/* Select the max points per category */
(SELECT
r.category_id,
min(r.record_time) AS best_time
FROM
cn_records r
GROUP BY
r.category_id) ar /* Actual records */
/* Join the records table to find the records that match the max points.
These are the only records to investigate */
INNER JOIN cn_records r
ON r.category_id = ar.category_id
AND r.record_time = ar.best_time
/* Inner (don't left) join player. What is a record without player */
INNER JOIN cn_players p ON r.player1_id = p.player_id
INNER JOIN cn_category c ON r.category_id = c.category_id
GROUP BY
r.category_id