我在网络上有一个非常好的看法,看起来很多人都有类似的问题,但我似乎无法找到解决问题的最终解决方案。
为了提供一些背景信息,我正在运行一个体育网站并拥有一个包含过去结果的数据库 - 涵盖数千条记录。
以下是一些示例数据 - (表名:匹配)
| match_id | manager_id | outcome |
| -------- | ---------- | ------- |
| 1 | 1 | W |
| 2 | 1 | D |
| 3 | 1 | D |
| 4 | 2 | L |
| 5 | 2 | L |
| 6 | 2 | W |
| 7 | 2 | D |
| 8 | 2 | L |
| 9 | 2 | L |
| 10 | 3 | W |
| 11 | 3 | W |
| 12 | 3 | W |
| 13 | 3 | W |
| 14 | 3 | D |
| 15 | 3 | D |
| 16 | 4 | L |
| 17 | 4 | L |
| 18 | 4 | D |
| 19 | 5 | W |
| 20 | 5 | W |
我想要做的是计算每个经理的匹配数以及结果 - 我通过使用此查询获得的结果。
SELECT
`manager_id`,
COUNT(*) AS `played`,
SUM(`outcome` = 'W') AS `won`,
SUM(`outcome` = 'D') AS `drawn`,
SUM(`outcome` = 'L') AS `lost`,
ROUND((SUM(`outcome` = 'W')/COUNT(*)) * 100, 0) AS `win_percentage`
FROM
`matches`
GROUP BY `manager_id`
查询结果
| manager_id | played | won | drawn | lost | win_percentage |
| ---------- | ------ | --- | ----- | ---- | -------------- |
| 1 | 3 | 1 | 2 | 0 | 33 |
| 2 | 6 | 1 | 1 | 4 | 16 |
| 3 | 6 | 4 | 2 | 0 | 66 |
| 4 | 3 | 0 | 1 | 2 | 0 |
| 5 | 2 | 2 | 0 | 0 | 100 |
这一切都很好,而且相对简单。
然而,我想要实现的是根据每个manager_id的第一个X记录数找出完全相同的数据。
例如,假设我想要每个经理的前两场比赛的上述数据。我应该得到如下结果。
| manager_id | played | won | drawn | lost | win_percentage |
| ---------- | ------ | --- | ----- | ---- | -------------- |
| 1 | 2 | 1 | 1 | 0 | 50 |
| 2 | 2 | 0 | 0 | 2 | 0 |
| 3 | 2 | 2 | 0 | 0 | 100 |
| 4 | 2 | 0 | 0 | 2 | 0 |
| 5 | 2 | 2 | 0 | 0 | 100 |
以上只是一个例子,实际上,我们将在每组搜索前50或100场比赛。
我非常感谢能够获得的任何支持。
答案 0 :(得分:1)
由于MySql中没有窗口函数,您可以使用this post中描述的变量。
set @howmany := 2;
SELECT
manager_id,
COUNT(*) AS played,
SUM(outcome = 'W') AS won,
SUM(outcome = 'D') AS drawn,
SUM(outcome = 'L') AS lost,
ROUND((SUM(outcome = 'W')/COUNT(*)) * 100, 0) AS win_percentage
FROM
(SELECT
match_id,
manager_id,
outcome,
CASE WHEN manager_id != @manager THEN @row := 1
ELSE @row := @row + 1 END as rownum,
CASE WHEN manager_id != @manager THEN @manager := manager_id
ELSE @manager END as _
FROM (SELECT * from matches ORDER BY manager_id, match_id) temp1
JOIN (SELECT @manager := 0) temp2
JOIN (SELECT @row := 0) temp3
) temp
WHERE temp.rownum <= @howmany
GROUP BY manager_id;