0

当我尝试时:

select * 
from some_table 
group by table_id 
order by timestamp desc;

我得到的行timestamp对于那个特定的值最小table_id(我用于分组)

我怎样才能获得该特定值具有最高timestamp值的行table_id

我也试过:

select * 
from some_table 
group by table_id 
having max(timestamp) 
order by timestamp desc;

这给出了与第一种情况相同的结果。

谢谢你

4

2 回答 2

1
select * 
from your_table t
inner join 
(
   select table_id, max(created_timestamp) as mts
   from your_table
   group by table_id 
) x on x.table_id = t.table_id 
    and x.mts = t.created_timestamp

在 MySQL 你可以做

select *, max(created_timestamp) as mts
from your_table
group by table_id 

但这并不能确保您获得相应的数据,max(created_timestamp)而只能获得您的table_id

于 2013-09-12T10:56:32.637 回答
0
SELECT * FROM (SELECT * FROM some_table ORDER BY timestamp) t1 GROUP BY t1.id
于 2013-09-13T06:38:51.917 回答