【发布时间】:2009-07-25 06:24:10
【问题描述】:
我目前正在尝试构建一个有点棘手的 MySQL Select 语句。这是我想要完成的任务:
我有一张这样的桌子:
data_table
uniqueID stringID subject
1 144 "My Subject"
2 144 "My Subject - New"
3 144 "My Subject - Newest"
4 211 "Some other column"
基本上,我想做的是能够通过 stringID 选择/分组(stringID 是线程的图片)而不是重复它。此外,我想选择最近的 stringID 行,(在上面的示例中是 uniqueID 3)。
因此,如果我要查询数据库,它将返回以下内容(最近的唯一 ID 在顶部):
uniqueID stringID subject
4 211 "Some other column"
3 144 "My Subject - Newest" //Notice this is the most recent and distinct stringID row, with the proper subject column.
我希望这是有道理的。谢谢你的帮助。
【问题讨论】:
-
您是否 100% 确定 uniqueID 将始终是表中的最高 ID?如果没有,我建议你添加一个最新的时间戳。
-
我实际上确实有一个时间戳列(不包括在我上面的示例中)。那么,我将如何使用我的时间戳列呢? MAX 是否适用于时间戳列?谢谢。
标签: mysql select greatest-n-per-group