最好不下载整个数据库并扫描每个项目,是否有可以访问此表的MySQL查询:
__________________________________
| ColID | Timestamp |
|___________|_____________________|
|_____2_____|_2012-08-01_12:00:00_|
|_____1_____|_2012-08-01_12:01:00_|
|_____3_____|_2012-08-01_12:02:00_|
|_____3_____|_2012-08-01_12:03:00_|
|_____2_____|_2012-08-01_12:04:00_|
|_____3_____|_2012-08-01_12:05:00_|
只返回这些行:
__________________________________
| ColID | Timestamp |
|___________|_____________________|
|_____1_____|_2012-08-01_12:01:00_|
|_____2_____|_2012-08-01_12:04:00_|
|_____3_____|_2012-08-01_12:05:00_|
只提取具有最高ColID
Timestamp
中的一个
答案 0 :(得分:1)
将GROUP BY
与MAX
:
SELECT ColID, MAX(Timestamp) AS Timestamp
FROM tbl
GROUP BY ColID
此外,您可能还需要留意未来的提示:如果您还要为每个最大ColID
选择可能位于同一个表中的其他列,则无法直接在由于GROUP BY
的性质,上述查询。您需要将查询包装在连接的subselect中,同时加入id和date列:
SELECT b.*
FROM tbl a
JOIN (
SELECT ColID, MAX(Timestamp) AS max_timestamp
FROM tbl
GROUP BY ColID
) b ON a.ColID = b.ColID AND a.max_timestamp = b.Timestamp
答案 1 :(得分:1)
是的,您可以将GROUP BY
与MAX
一起使用,以实现此目的:
SELECT ColID, MAX(Timestamp) AS max_Timestamp
FROM my_table
GROUP BY ColID;