1

我有一张桌子

**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
4

7 回答 7

1

GROUP BY在您的专栏的日期部分使用date(这似乎是 type DATETIME):

SELECT user_id, MAX(date) FROM table GROUP BY DATE(date);
于 2012-12-06T08:12:48.963 回答
0

要获取一位用户的最新日期时间,请尝试此操作

SELECT user_id FROM TB_NAME ORDER BY date DESC LIMIT 0,1
于 2012-12-06T08:14:30.000 回答
0

如果您只需要一个用户

SELECT 
    MAX(date) as LatestDateTime
FROM 
    mytable

对于多个用户

SELECT 
    *
FROM 
    mytable
ORDER By date DESC
于 2012-12-06T08:15:09.457 回答
0

尝试:

SELECT user_id, max(date) from user group by user_id

或者

select user_id from user where date = (select max(date) from user)
于 2012-12-06T08:15:22.310 回答
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)

祝你好运!

于 2012-12-06T08:52:13.507 回答
0

尝试这个:

select * from (
   select user_id, date
   from mytable
   order by date desc ) x
group by date(date);

注意:这是一个 mysql-only 解决方案,但它有效!

于 2012-12-06T08:40:24.133 回答
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
于 2012-12-06T08:43:03.127 回答