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
WHERE log_time >= DATE_SUB(CURRENT_DATE, INTERVAL $minutes MINUTE)
GROUP BY job_id
It only selects the recent records and skips the old. When I change log_time <= ...
it only selects old and skips which are the new one.
What am I doing wrong?
MySQL interval is an operator, which is based on the binary search algorithm to search the items and returns the value from 0 to N. It is mainly used to calculate the date and time values. We can use the following syntax to create an interval value: INTERVAL expr unit.
MySQL CURDATE() Function The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). Note: This function equals the CURRENT_DATE() function.
The DATE_ADD() function adds a time/date interval to a date and then returns the date.
UNIX_TIMESTAMP() : This function in MySQL helps to return a Unix timestamp. We can define a Unix timestamp as the number of seconds that have passed since '1970-01-01 00:00:00'UTC. Even if you pass the current date/time or another specified date/time, the function will return a Unix timestamp based on that.
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`
NOW()
for CURRENT_DATE just means 2010-08-04, not including the time<
to get entries older than that date.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With