MySQL INTERVAL Mins

后端 未结 3 1194
天涯浪人
天涯浪人 2020-12-30 07:36

I am trying to get the all records which are 2 hours or more old using this query:

$minutes = 60 * 2

SELECT COUNT(id) AS TOTAL, job_id 
  from tlb_stats 
 W         


        
相关标签:
3条回答
  • 2020-12-30 08:16

    You can also do it in this way:

    $minutes = 60 * 2
    
    SELECT COUNT(`id`) AS `TOTAL`,
           `job_id` 
    FROM `tlb_stats` 
    WHERE `log_time` < NOW() - INTERVAL $minutes MINUTE
    GROUP BY `job_id`
    
    0 讨论(0)
  • 2020-12-30 08:18
    SELECT * FROM `table_name` WHERE CURTIME() >= (`colname` + INTERVAL 120 MINUTE)
    

    Here, colname is the column where you added timestamp at the time when the record was created.

    0 讨论(0)
  • 2020-12-30 08:41

    Try:

    $minutes = 60 * 2
    
    SELECT COUNT(`id`) AS `TOTAL`, `job_id` 
      FROM `tlb_stats` 
      WHERE `log_time` < DATE_SUB(NOW(), INTERVAL $minutes MINUTE) 
      GROUP BY `job_id`
    
    • use backticks to quote fields (words like "total" and "id" may someday mean something in MySQL)
    • use NOW() for CURRENT_DATE just means 2010-08-04, not including the time
    • use < to get entries older than that date.
    0 讨论(0)
提交回复
热议问题