去年10結果
http://sqlfiddle.com/#!9/3d586/22
--get the minute component of the update time
select minute(updated_at) as Sec
--count the number of records which have this minute
, count(1) as Cnt
from myTable
--use group by to ensure we return 1 row per minute
group by minute(updated_at)
--list from most recent working backwards
order by minute(updated_at) desc
--return up to 10 results
limit 10
結果最後10分鐘
http://sqlfiddle.com/#!9/3d586/26
--get the minute component of the update time
select minute(y.d) as Min
--count the number of records which have this minute
--use m.id instead of 1 or * to ensure where there's no result from myTable
--we don't count any rows
, count(m.id) as Cnt
from
(
--get the current date's minute, offset by a given amount
select date_add(now(), interval -x.a minute) d
from
(
--the list of given amounts by which to offset the above date
select 0 a
union select 1
union select 2
union select 3
union select 4
union select 5
union select 6
union select 7
union select 8
union select 9
) x
) y
--left join to ensure the above list drives which results we get,
--regardless of whether there are matching entries in myTable
left outer join myTable m
--join on the minute of each date
on minute(m.updated_at) = minute(y.d)
--use group by to ensure we return 1 row per minute
group by minute(y.d)
--list from most recent working backwards
order by minute(y.d) desc
http://stackoverflow.com/a/13761926/2589202 – paqogomez