MySQL INTERVAL Mins

I am trying to get all records that are 2 hours or older using this query

$minutes = 60 * 2

SELECT COUNT(id) AS TOTAL, job_id 
  from tlb_stats 
 WHERE log_time >= DATE_SUB(CURRENT_DATE, INTERVAL $minutes MINUTE) 
GROUP BY job_id

He selects only the latest entries and skips the old ones. When I change log_time <=, it selects only the old and skips that are new.

What am I doing wrong?

+5
source share
2 answers

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 backlinks to quote fields (words like "total" and "id" might someday mean something in mysql)
  • use NOW()for CURRENT_DATE, just means 2010-08-04, not including time
  • use <to get records older than this date.
+11
source

SELECT * FROM table_name WHERE CURTIME() >= (colname + INTERVAL 120 MINUTE)

, colname - ,

0

All Articles