SQL-分组依据和排序依据最小值

SQL-分组依据和排序依据最小值,sql,group-by,sql-order-by,Sql,Group By,Sql Order By,我有一张球员成绩表: id | id_user | timems | status | cancel 玩家在两种状态中选择:开始游戏时为1或2。Im对最佳时间进行排名: SELECT *, MIN(timems) AS besttime FROM `wyniki` WHERE NOT cancel=1 GROUP BY id_user ORDER BY MIN(timems) ASC 这很好,但我不知道什么是“状态”的“最佳时间”。选择id\u用户, SELECT id_user,

我有一张球员成绩表:

id | id_user | timems | status | cancel
玩家在两种状态中选择:开始游戏时为1或2。Im对最佳时间进行排名:

SELECT *, MIN(timems) AS besttime 
FROM `wyniki` 
WHERE NOT cancel=1 
GROUP BY id_user 
ORDER BY MIN(timems) ASC
这很好,但我不知道什么是“状态”的“最佳时间”。

选择id\u用户,
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
最小值(状态为1时,则timems else null end)作为最佳时间1, 最小值(状态为2时,则timems else null end)作为最佳时间2 来自wyniki 哪里取消1 按id\u用户分组 按分钟(时间)订购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

groupby
用法是无效的标准SQL。您使用的是哪种DBMS产品?