Get time interval in mysql

Do I have a request for a time interval - one minute, five minutes, a quarter of an hour, half an hour, an hour and a day? I use MySQL as a database.

+7
mysql time intervals
source share
2 answers

to get a range, for example, 30 to 45 minutes ago, do this

SELECT * FROM tbl WHERE tbl.mydate > DATE(DATE_sub(NOW(), INTERVAL 45 MINUTE)) AND tbl.mydate < DATE(DATE_sub(NOW(), INTERVAL 30 MINUTE)); 
+8
source share

Perhaps you are looking for date_sub :

 SELECT * FROM YOURTABLE t WHERE t.timestamp > date_sub(NOW(), interval 1 hour); 

For different intervals, you can change 1 hour to 5 days, 5 weeks, etc.).

From the documentation:

DATE_SUB (date, unit of measure INTERVAL)

The date argument indicates the start date or date and time value. expression is an expression indicating the added value of an interval or is subtracted from the start date. expr is a string; it may begin with a "-" for negative intervals. unit is a keyword indicating the units in which the expression is to be interpreted.

The following table shows the expected form of the expr argument for each unit value.

 unit Value Expected expr Format MICROSECOND MICROSECONDS SECOND SECONDS MINUTE MINUTES HOUR HOURS DAY DAYS WEEK WEEKS MONTH MONTHS QUARTER QUARTERS YEAR YEARS 
+3
source share

All Articles