有一张表:
+---------+--------+---------------------+----------+
| user_id | marker | date | trans_id |
+---------+--------+---------------------+----------+
| 6 | M | 2013-08-27 11:45:24 | 5 |
| 6 | MA | 2013-08-27 11:45:42 | 6 |
| 6 | A | 2013-08-27 11:45:59 | 7 |
+---------+--------+---------------------+----------+
我测试了查询:SELECT marker , MAX(date) AS maxdate
从我的表
其中 user_id =6
但这是不合适的。 你将如何编写查询? 提前致谢。
最佳答案
这将为您提供每个 user_id
的最新记录
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT user_id , MAX(date) date
FROM tableName
GROUP BY user_ID
) b ON a.user_id = b.user_id AND
a.date = b.date
-- WHERE a.user_id = 6 ==> if you want for specific user_id only
关于mysql - 如何通过 MAX(date) 检索行数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18460173/