MySQL,最后X个小时内返回所有结果 [英] MySQL, return all results within X last hours

查看:70
本文介绍了MySQL,最后X个小时内返回所有结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表由多个列组成,包括unixtimestamp:

My table is composed of multiple columns, including unixtimestamp:

CREATE MyTable(
   id INT NOT NULL AUTO_INCREMENT,
   ...
   timeStart INT(11)
)ENGINE=MyISAM;

新项目将连续插入到该表中.

New items are inserted continually to this table.

我需要返回所有带有时间戳的项目>(MAX(startTime)-X_duration)

I need to return all items with a timestamp > (MAX(startTime)-X_duration)

我有兴趣编写一个最佳查询来实现它.

I am interested to write a single and optimal query to achieve it.

  1. 一种简单的方法是编写嵌套的select,内部select返回MAX(timeStart) as maxTime,而不是选择所有大于where子句的记录.
  1. One easy way to do it, is to write nested select, where the inner select returns the MAX(timeStart) as maxTime , than select all records greater than in where clause.

我是否有更好更好的解决方案?

I am interested if there are better and more efficient solutions?

推荐答案

SELECT  *
FROM    tableName
WHERE   timeStart > (SELECT MAX(timeStart) + INTERVAL -1 SECOND FROM tableName)

  • SQLFiddle演示
    • SQLFiddle Demo
    • 这篇关于MySQL,最后X个小时内返回所有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆