我有一张名为'记分牌'的桌子,其中包含以下内容
+-----------+----------+--------+---------------------+
| Team_name | Level | Status | completed_time |
+-----------+----------+--------+---------------------+
| sample1 | 1 | yes | 2011-09-11 15:15:44 |
| sample2 | 1 | yes | 2011-09-11 15:48:13 |
| sample2 | 2 | yes | 2011-09-11 15:50:13 |
| sample2 | 3 | yes | 2011-09-11 15:52:13 |
| sample1 | 2 | yes | 2011-09-11 15:16:52 |
| sample1 | 3 | yes | 2011-09-11 15:31:06 |
| sample1 | 4 | yes | 2011-09-11 15:31:47 |
| sample1 | 5 | no | 2011-09-11 15:31:47 |
| sample2 | 4 | no | 2011-09-11 15:52:13 |
| sample4 | 1 | yes | 2011-09-12 17:11:51 |
| sample4 | 2 | yes | 2011-09-12 17:12:18 |
| sample4 | 3 | yes | 2011-09-12 17:16:53 |
| sample4 | 4 | yes | 2011-09-13 22:58:24 |
| sample4 | 5 | yes | 2011-09-14 03:03:21 |
| sc102 | 1 | yes | 2011-09-14 01:42:20 |
| sc102 | 2 | no | 2011-09-14 01:42:20 |
| sample4 | 6 | yes | 2011-09-14 03:08:20 |
| sample4 | 7 | no | 2011-09-14 03:08:20 |
| Javad | 1 | yes | 2011-09-14 21:07:05 |
| Javad | 2 | yes | 2011-09-14 21:08:47 |
| Javad | 3 | yes | 2011-09-14 21:18:40 |
| Javad | 4 | yes | 2011-09-14 21:25:12 |
| Javad | 5 | yes | 2011-09-14 21:34:25 |
| Javad | 6 | yes | 2011-09-14 21:41:33 |
| Javad | 7 | yes | 2011-09-14 21:47:31 |
| Javad | 8 | yes | 2011-09-14 21:48:15 |
| Javad | 9 | yes | 2011-09-14 21:48:25 |
| Javad | 10 | yes | 2011-09-14 21:49:07 |
| SCAV527 | 1 | yes | 2011-12-05 21:15:30 |
| SCAV527 | 2 | yes | 2011-12-05 21:20:30 |
| SCAV527 | 3 | yes | 2011-12-05 21:20:46 |
| SCAV527 | 4 | no | 2011-12-05 21:20:46 |
+-----------+----------+--------+---------------------+
我需要根据团队及其completed_time解决的等级来准备游戏等级列表。如果多个团队解决了一个级别,则优先级将归于首先根据completed_time解决的团队。准备等级列表时,应省略状态为“否”的行。
查询的输出应包含team_name,level,completed_time等字段。
你能帮我写一个排名列表的SQL查询吗?我使用MySQL作为后端。
查询的输出应该是这样的。
+-----------+-----------+----------+---------------------+
| Rank | Team_name | Level | completed_time |
+-----------+-----------+----------+---------------------+
| 1 | Javad | 10 | 2011-09-14 21:49:07 |
| 2 | sample4 | 6 | 2011-09-14 03:08:20 |
| 3 | sample1 | 4 | 2011-09-11 15:31:47 |
| 4 | SCAV527 | 3 | 2011-09-10 21:20:46 |
| 5 | sample2 | 3 | 2011-09-11 15:52:13 |
| 6 | sc102 | 1 | 2011-09-14 01:42:20 |
+-----------+-----------+----------+---------------------+
答案 0 :(得分:0)
这样的事情可能有所帮助:
SELECT v1.Team_name, v1.Level , COUNT(v2.Level) AS Rank FROM scoreboard v1 JOIN scoreboard v2 ON v1.Level < v2.Level OR (v1.Level =v2.Level and v1.Team_name = v2.Team_name) WHERE v1.status = "yes" GROUP BY v1.Team_name, v1.Level ORDER BY v1.Level DESC, v1.Team_name DESC;
答案 1 :(得分:0)
SELECT @rank:=@rank+1 as rank, r.*
FROM (
SELECT team_name, MAX(level) as level, MAX(completed_time) as completed_time
FROM results
WHERE `status` = 'yes'
GROUP BY team_name
ORDER BY 2 DESC) as r, (SELECT @rank:=0) as init