我有一张桌子
**user_id** | **date**
1 | 2012-10-18 09:34:4
3 | 2012-10-18 09:34:54
5 | 2012-10-17 09:14:54
6 | 2012-10-17 09:13:24
4 | 2012-10-16 08:34:54
9 | 2012-10-16 08:34:54
我想要的是具有最新日期时间的user_id,即
**user_id** | **date**
3 | 2012-10-18 09:34:54
5 | 2012-10-17 09:14:54
4 | 2012-10-16 08:34:54
答案 0 :(得分:1)
在GROUP BY
列的日期部分使用date
(似乎类型为DATETIME
):
SELECT user_id, MAX(date) FROM table GROUP BY DATE(date);
答案 1 :(得分:0)
要让一位用户获得最新的日期时间,请尝试使用
SELECT user_id FROM TB_NAME ORDER BY date DESC LIMIT 0,1
答案 2 :(得分:0)
如果您只需要一个用户
SELECT
MAX(date) as LatestDateTime
FROM
mytable
对于多个用户
SELECT
*
FROM
mytable
ORDER By date DESC
答案 3 :(得分:0)
尝试:
SELECT user_id, max(date) from user group by user_id
或
select user_id from user where date = (select max(date) from user)
答案 4 :(得分:0)
试试这个:
select * from (
select user_id, date
from mytable
order by date desc ) x
group by date(date);
注意:这是一个仅限mysql的解决方案,但它有效!
答案 5 :(得分:0)
另一个SQL Fiddle
select user_id, `date`
from mytable t1
join (select max(`date`) as max_date
from mytable
group by date(`date`)) t2 on t1.`date` = t2.max_date
答案 6 :(得分:0)
只是一个猜测,我希望它有效。
SELECT t.user_id, t.date
FROM table t
WHERE t.date = (SELECT MAX(t2.date) FROM table t2 GROUP BY t2.date HAVING t2.date = t.date)
古德勒克!