如何在MySQL中根据时间间隔选择行? [英] How to select rows according to time interval in mysql?

查看:72
本文介绍了如何在MySQL中根据时间间隔选择行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有这样时间戳的数据库:

I have a DB that have timestamp like this :

2018-10-25 13:00:00 
2018-10-25 15:00:10 
2018-10-25 16:10:00 
2018-10-25 17:00:00 

现在我只想选择过去一小时的日期 我尝试运行此-

Now I want to select only date from the last hour I have tried to run this -

SELECT * FROM sample.writing where (date(Date) = interval 1 hour )

怎么了? 日期是DateTime类型.

What is wrong ? Date is DateTime type.

谢谢:)

推荐答案

  • 首先,Date BETWEEN low-value AND high-value 比较运算符,以获取指定范围内的值.
    • Firstly, Date is a Keyword in MySQL. You should really avoid using it as a table/column name.
    • To get a time 1 hour before the current time, you can add an INTERVAL -1 HOUR to current time (NOW()), using Date_add() function.
    • We can use BETWEEN low-value AND high-value comparison operator, to get the values from the prescribed range.
    • 尝试以下查询:

      SELECT * 
      FROM sample.writing 
      where Date BETWEEN DATE_ADD(NOW(), INTERVAL -1 HOUR) AND NOW()
      

      这篇关于如何在MySQL中根据时间间隔选择行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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