【发布时间】:2021-07-12 02:56:45
【问题描述】:
我需要选择用户最近参加的 5 场比赛,但我不希望有重复。所以我需要一条 SQL 行,将它们按从最近到最少的顺序排列,但还要确保没有不同的。要订购它们,我有一个时间变量,但如果我使用:
select distinct name, image, time
from history
where userID = USERID
order by time desc
这会删除除第一个时间之外的所有其他时间,这意味着我将按照最先玩的游戏的顺序获得结果。
如果你还是不明白这里是架构和我有的一些数据:
架构:
CREATE TABLE history (userID integer, name text not null, image text not null, time text not null);
数据:
6 | Arcade Mathematicians | arcadeMathematicians | 2021-04-17 07:59:00
6 | Arcade Mathematicians | arcadeMathematicians | 2021-04-17 08:01:04
6 | Infinite Spinner | infiniteSpinner | 2021-04-17 08:01:08
6 | Arcade Mathematicians | arcadeMathematicians | 2021-04-17 08:01:12
6 | Arcade Mathematicians | arcadeMathematicians | 2021-04-17 08:02:13
6 | Infinite Spinner | infiniteSpinner | 2021-04-17 08:02:16
6 | Arcade Mathematicians | arcadeMathematicians | 2021-04-17 08:03:16
6 | Infinite Spinner | infiniteSpinner | 2021-04-17 08:03:18
6 | Arcade Mathematicians | arcadeMathematicians | 2021-04-17 08:03:21
因此,如果您在此处看到如果我按时使用 distinct,它将无法正常工作,因为仍然会显示所有内容。
【问题讨论】:
-
(1) 用您正在使用的数据库标记您的问题。 (2) 您的描述涉及一种叫做“游戏”的东西,但您的数据库中没有这样的实体或列。
标签: sql sql-order-by distinct