SQL语句如下:
SELECT if(timestampdiff(hour, sa.add_time, now()) < 1000,
if(timestampdiff(hour, sa.add_time, now()) < 300,
if(timestampdiff(hour, sa.add_time, now()) < 128, 3 , 2 ) , 1 ) , 0 ) diftime
from DB_table sa
order by diftime desc
3,2,1,0 就是根据不同条件返回的数据
这里是计算的两个时间间隔的差的小时数
hour是指小时,可以替换成minute,day ,month , year 不做详细的验证,可以自己练习