mySQL 尝试获取过去 24 小时和过去 60 分钟的帖子失败
好的,正在编辑此...
SELECT *
FROM votelog
WHERE ipaddress = '127.0.0.1'
AND datevoted
BETWEEN DATE_SUB( CURDATE( ) , INTERVAL 24 HOUR )
AND CURDATE( )
LIMIT 0 , 30
这是我尝试运行以查找过去 24 小时内的帖子的查询示例。在过去的 60 分钟内,我还针对不同的需求运行了单独的一个。问题是我正在测试的表中至少有 4 行,其中 3 行属于 24 小时条款。
编辑
好吧,所以我弄清楚了我的问题,1我太累了.. 2 Between和Date_Sub的可怕使用..直到现在我才意识到我应该使用我有CURDATE()的col名称下面回答我自己的问题。
这就是数据库中的时间戳的样子,标准 DATETIME.. 2011-09-01 13:20:08
话虽如此,我没有产生任何结果。
Ok Editing this...
SELECT *
FROM votelog
WHERE ipaddress = '127.0.0.1'
AND datevoted
BETWEEN DATE_SUB( CURDATE( ) , INTERVAL 24 HOUR )
AND CURDATE( )
LIMIT 0 , 30
That is an example of the query I am attempting to run to find posts within the past 24 hours. I am also running a separate one for different needs for in the past 60 minutes. Issue is there is at least 4 rows in the table I am testing with 3 of which fall under the 24 hour clause.
Edit
Ok so I figured out my problem, 1 Im to damn tired.. 2 Horrible use of Between and Date_Sub.. It didn't dawn on me till now I should have been using the col name where I have CURDATE() going to answer my own question below.
this is what the timestamp in the DB looks like, standard DATETIME.. 2011-09-01 13:20:08
with that being said I am yielding no results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您没有正确使用 BETWEEN,正确的语法是:
您应该将查询的结尾更改为:
或使用
>
运算符。You are not using BETWEEN correctly, the correct syntax is:
you should change the end of your query to:
or use
>
operator.你试过这个吗?
BETWEEN
需要一个AND
你知道......我认为你总是可以这样做
Have you tried this? The
BETWEEN
needs anAND
you know...And I think you could always do it like this
编辑:由于 ID 不是字符串类型,因此不用
mysql_real_escape_string($_GET['id'])
,只需使用(int)$_GET['id']
即可。Edited: Since ID is not string type, instead of
mysql_real_escape_string($_GET['id'])
, just use(int)$_GET['id']
.所以经过长时间的凝视,这就是我的结果......我现在感觉很脏......
So after much staring this is my result... i feel dirty now..
您可能没有日期,但有日期时间字段(或时间戳,略有不同)。不要使用
CURDATE()
。在您的查询中,CURDATE()
给出2011-09-03
(日期!),当它与您的日期时间字段进行比较时,它被视为2011- 09-03 00:00:00
(午夜!),因此您的查询(如果今天运行)与:所以,与:
这就是为什么您会丢失从午夜过去到现在的所有记录。
使用
NOW()
:有用的读物:
MySQL 文档:DATETIME、DATE 和 TIMESTAMP 类型
MySQL 文档:日期和时间函数
所以问题:日期时间与时间戳
You probably don't have a date but a datetime field (or a timestamp, which is slightly different). Don't use
CURDATE()
. In your query,CURDATE()
gives2011-09-03
(a date!) and when it is compared to your datetime field it is treated as2011-09-03 00:00:00
(midnight!), so your query (if run today) same as:so, same as:
That's why you lose any records that are between passed midnight and now.
Use
NOW()
:Useful readings:
MySQL docs: The DATETIME, DATE, and TIMESTAMP Types
MySQL docs: Date and Time functions
SO question: datetime vs timestamp