获取最新 5 个独特条目
我有一个 Access 数据库,我正在使用具有以下结构的 JET 与之交互:
ID UsersID
1 1
2 2
3 2
4 3
5 1
我想要做的是获取最新的唯一条目。例如:
ID UsersID
5 1
4 3
3 2
但是,我在混合分组和排序时遇到了麻烦。我尝试过
SELECT DISTINCT [UsersID] FROM [Table] ORDER BY [ID] DESC
SELECT [UsersID] FROM [Table] GROUP BY [UsersID] ORDER BY [ID] DESC
但是,没有运气。注意:如果我从任一查询中省略 ORDER BY [ID] DESC
,它会起作用,但显然排序不符合预期。
I have an Access database that I am interacting with using JET with the following structure:
ID UsersID
1 1
2 2
3 2
4 3
5 1
What I am trying to do is get the latest unique entries. eg:
ID UsersID
5 1
4 3
3 2
However, I am having trouble mixing grouping and ordering. I have tried
SELECT DISTINCT [UsersID] FROM [Table] ORDER BY [ID] DESC
SELECT [UsersID] FROM [Table] GROUP BY [UsersID] ORDER BY [ID] DESC
But, no luck. Note: if I leave off the ORDER BY [ID] DESC
from either query, it works, but obviously the ordering is not as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过简单的 GROUP BY 查询获取每个 UserID 的最大 ID 值。
但是我不确定我是否理解 order by 和最新的 5 个要求。如果latest 5 表示上述查询中的5 个最大ID 值,那么请尝试这种方式...
“Table”不是表的最佳名称。希望您的真实情况使用不同的表名称。 :-)
You can get the largest ID value for each UserID with a simple GROUP BY query.
However I'm not sure I understand the order by and latest 5 requirements. If latest 5 means the 5 largest ID values from the above query, then try it this way ...
"Table" is not the best name for a table. Hope your real world situation uses a different table name. :-)