【发布时间】:2010-10-29 19:08:47
【问题描述】:
我有下表:
id time text otheridentifier
-------------------------------------------
1 6 apple 4
2 7 orange 4
3 8 banana 3
4 9 pear 3
5 10 grape 2
我想要做的是选择 3 个最近的记录(按时间降序),它们的 otheridentifiers 是不同的。所以在这种情况下,结果将是 id's: 5、4 和 2。
id = 3 将被跳过,因为有一个更新的记录具有相同的 otheridentifier 字段。
这是我尝试做的:
SELECT * FROM `table` GROUP BY (`otheridentifier`) ORDER BY `time` DESC LIMIT 3
但是,我最终得到了 id = 5、3 和 1 的行,而不是预期的 5、4、2。
有人能告诉我为什么这个查询不会返回我所期望的吗?我尝试将 ORDER BY 更改为 ASC,但这只是将返回的行重新排列为 1、3、5。
【问题讨论】:
标签: mysql group-by sql-order-by