从mysql表中的列中选择max,min,last值

G J*_*Jay 3 mysql sql

表格式

id   time_stamp
3    2013-09-05 12:00:00
5    2013-09-06 12:00:00 
12   2013-09-07 12:00:00
2    2013-09-08 12:00:00
5    2013-09-09 12:00:00
8    2013-09-10 12:00:00
Run Code Online (Sandbox Code Playgroud)

从上面的表中我想在单个mysql select查询语句中选择min(id),max(id),last id,last time_stamp

需要的输出是:

min   max  last(val)  last(time_stamp)
2     12   8          2013-09-09 12:00:00
Run Code Online (Sandbox Code Playgroud)

我使用了以下查询

select id, min(id),max(id), time_stamp from table order by time_stamp limit 1
Run Code Online (Sandbox Code Playgroud)

我得到错误的最新id值3而不是8

如果下面的SQL小提琴http://www.sqlfiddle.com/#!2/e9cb1/2/0,请检查这一点

Gor*_*off 7

这是一个方法,使用substring_index()/ group_concat()trick:

select min(id), max(id),
       substring_index(group_concat(id order by time_stamp desc), ',', 1) as lastid
from table ;
Run Code Online (Sandbox Code Playgroud)