SQL - GROUP BY 和 ORDER BY MIN

SQL - GROUP BY and ORDER BY MIN

我有 table 个玩家结果:

id | id_user | timems | status | cancel

玩家在开始游戏时选择两种状态:1 或 2。我在做最佳时间排名:

SELECT *, MIN(timems) AS besttime 
FROM `wyniki` 
WHERE NOT cancel=1 
GROUP BY id_user 
ORDER BY MIN(timems) ASC

这很好,但我不知道 'besttime' 是什么 'status'。

SELECT id_user, 
       MIN(case when status = 1 then timems else null end) AS best_time_1,
       MIN(case when status = 2 then timems else null end) AS best_time_2
FROM wyniki 
WHERE cancel <> 1 
GROUP BY id_user 
ORDER BY MIN(timems) ASC

此查询容易出错

你可以做到

SQL 查询:

SELECT id_user, status , MIN(timems) AS besttime FROM `wyniki` 
WHERE NOT cancel=1 GROUP BY id_user, status ORDER BY MIN(timems) ASC