Home  >  Article  >  Backend Development  >  php如何读取mysql最近1个小时的记录

php如何读取mysql最近1个小时的记录

WBOY
WBOYOriginal
2016-06-06 20:51:23959browse

时间字段的格式是这样的 2012-10-22 17:00:00

我想读取最近1个小时新增的记录,$time_limit = 1 * 60 * 60;

然后查询:

"SELECT * FROM Comments
WHERE TimeStamp > NOW() - $time_limit"

不过没有反应啊,问题出在哪了呢

回复内容:

时间字段的格式是这样的 2012-10-22 17:00:00

我想读取最近1个小时新增的记录,$time_limit = 1 * 60 * 60;

然后查询:

"SELECT * FROM Comments
WHERE TimeStamp > NOW() - $time_limit"

不过没有反应啊,问题出在哪了呢

在字段上用函数会到帐索引失效, 你可以这样做

换个思路解决

$timedate = date('Y-m-d H:i:s',time());
$hourtime = date('Y-m-d H:i:s',time()-3600);

SELECT * FROM Comments  WHERE TimeStamp  between $timedate  and $timedate;

TimeStamp为啥不建int,却用datetime类型的。其实楼上可以少一个条件,当前时间不需要做限制的。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn