我正在尝试使用此查询获取所有2小时或更长时间的记录:
$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
它只选择最近的记录并跳过旧记录。当我更改log_time <= ...
时,它只选择旧的并跳过新的。
我做错了什么?
答案 0 :(得分:18)
尝试:
$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()
进行CURRENT_DATE仅表示2010-08-04,不包括时间<
获取旧的条目。答案 1 :(得分:1)
SELECT * FROM `table_name` WHERE CURTIME() >= (`colname` + INTERVAL 120 MINUTE)
此处,colname
是您在创建记录时添加时间戳的列。
答案 2 :(得分:0)
您也可以通过以下方式做到这一点:
$minutes = 60 * 2
SELECT COUNT(`id`) AS `TOTAL`,
`job_id`
FROM `tlb_stats`
WHERE `log_time` < NOW() - INTERVAL $minutes MINUTE
GROUP BY `job_id`