MySQL INTERVAL 分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3401551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL INTERVAL Mins
提问by Maximus
I am trying to get the all records which are 2 hours or more old using this query:
我正在尝试使用此查询获取 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
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.
它只选择最近的记录并跳过旧的记录。当我更改时log_time <= ...,它只选择旧的并跳过新的。
What am I doing wrong?
我究竟做错了什么?
回答by mvds
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 meansomething in MySQL)
- use
NOW()for CURRENT_DATE just means 2010-08-04, not including the time - use
<to get entries olderthan that date.
- 使用反引号来引用字段(像“total”和“id”这样的词可能有一天在 MySQL 中意味着某些东西)
- 使用
NOW()了CURRENT_DATE只是意味着2010-08-04,不包括时间 - 用于
<获取早于该日期的条目。
回答by DeathRs
SELECT * FROM `table_name` WHERE CURTIME() >= (`colname` + INTERVAL 120 MINUTE)
Here, colnameis the column where you added timestamp at the time when the record was created.
此处colname是您在创建记录时添加时间戳的列。
回答by simhumileco
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`

