mySQL 尝试获取过去 24 小时和过去 ​​60 分钟的帖子失败

发布于 2024-12-02 18:52:09 字数 478 浏览 0 评论 0原文

好的,正在编辑此...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

两个我 2024-12-09 18:52:09

您没有正确使用 BETWEEN,正确的语法是:

表达式介于最小值和最大值之间

您应该将查询的结尾更改为:

...BETWEEN DATE_SUB(CURDATE(), INTERVAL 24 hour) AND CURDATE()

或使用 > 运算符。

You are not using BETWEEN correctly, the correct syntax is:

expr BETWEEN min AND max

you should change the end of your query to:

...BETWEEN DATE_SUB(CURDATE(), INTERVAL 24 hour) AND CURDATE()

or use > operator.

若沐 2024-12-09 18:52:09

你试过这个吗? BETWEEN 需要一个 AND 你知道......

$query = "SELECT * FROM votelog WHERE ID=".mysql_real_escape_string($_GET['id'])." AND ipaddress='".mysql_real_escape_string(getRealIpAddr())."' AND datevoted BETWEEN DATE_SUB(CURDATE(), INTERVAL 24 HOUR) AND DATE_SUB(CURDATE(), INTERVAL 60 MINUTE)"; 

我认为你总是可以这样做

 $query = "SELECT * FROM votelog WHERE ID=".mysql_real_escape_string($_GET['id'])." AND ipaddress='".mysql_real_escape_string(getRealIpAddr())."' AND datevoted >= DATE_SUB(CURDATE(), INTERVAL 24 HOUR) AND datevoted <= DATE_SUB(CURDATE(), INTERVAL 60 MINUTE)"; 

Have you tried this? The BETWEEN needs an AND you know...

$query = "SELECT * FROM votelog WHERE ID=".mysql_real_escape_string($_GET['id'])." AND ipaddress='".mysql_real_escape_string(getRealIpAddr())."' AND datevoted BETWEEN DATE_SUB(CURDATE(), INTERVAL 24 HOUR) AND DATE_SUB(CURDATE(), INTERVAL 60 MINUTE)"; 

And I think you could always do it like this

 $query = "SELECT * FROM votelog WHERE ID=".mysql_real_escape_string($_GET['id'])." AND ipaddress='".mysql_real_escape_string(getRealIpAddr())."' AND datevoted >= DATE_SUB(CURDATE(), INTERVAL 24 HOUR) AND datevoted <= DATE_SUB(CURDATE(), INTERVAL 60 MINUTE)"; 
爱格式化 2024-12-09 18:52:09
$query = "SELECT * FROM votelog WHERE ID=".(int)$_GET['id']." AND ipaddress='".mysql_real_escape_string(getRealIpAddr())."' AND datevoted > DATE_SUB(CURDATE(), INTERVAL 24 hour)";

编辑:由于 ID 不是字符串类型,因此不用 mysql_real_escape_string($_GET['id']),只需使用 (int)$_GET['id'] 即可。

$query = "SELECT * FROM votelog WHERE ID=".(int)$_GET['id']." AND ipaddress='".mysql_real_escape_string(getRealIpAddr())."' AND datevoted > DATE_SUB(CURDATE(), INTERVAL 24 hour)";

Edited: Since ID is not string type, instead of mysql_real_escape_string($_GET['id']), just use (int)$_GET['id'].

地狱即天堂 2024-12-09 18:52:09
SELECT *
FROM votelog
WHERE ipaddress = '127.0.0.1'
AND 
DATE_SUB( datevoted , INTERVAL 24 HOUR )
LIMIT 0 , 30

所以经过长时间的凝视,这就是我的结果......我现在感觉很脏......

SELECT *
FROM votelog
WHERE ipaddress = '127.0.0.1'
AND 
DATE_SUB( datevoted , INTERVAL 24 HOUR )
LIMIT 0 , 30

So after much staring this is my result... i feel dirty now..

风启觞 2024-12-09 18:52:09

您可能没有日期,但有日期时间字段(或时间戳,略有不同)。不要使用CURDATE()。在您的查询中,CURDATE() 给出 2011-09-03(日期!),当它与您的日期时间字段进行比较时,它被视为 2011- 09-03 00:00:00(午夜!),因此您的查询(如果今天运行)与:

SELECT *
FROM votelog
WHERE ipaddress = '127.0.0.1'
AND datevoted
BETWEEN DATE_SUB( `2011-09-03 00:00:00` , INTERVAL 24 HOUR )
AND `2011-09-03 00:00:00`
LIMIT 0 , 30

所以,与:

SELECT *
FROM votelog
WHERE ipaddress = '127.0.0.1'
AND datevoted
BETWEEN `2011-09-02 00:00:00` AND `2011-09-03 00:00:00`
LIMIT 0 , 30

这就是为什么您会丢失从午夜过去到现在的所有记录。


使用 NOW()

SELECT *
FROM votelog
WHERE ipaddress = '127.0.0.1'
  AND datevoted
      BETWEEN DATE_SUB( NOW( ) , INTERVAL 24 HOUR )
          AND NOW( )
LIMIT 0 , 30

有用的读物​​:

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() gives 2011-09-03 (a date!) and when it is compared to your datetime field it is treated as 2011-09-03 00:00:00 (midnight!), so your query (if run today) same as:

SELECT *
FROM votelog
WHERE ipaddress = '127.0.0.1'
AND datevoted
BETWEEN DATE_SUB( `2011-09-03 00:00:00` , INTERVAL 24 HOUR )
AND `2011-09-03 00:00:00`
LIMIT 0 , 30

so, same as:

SELECT *
FROM votelog
WHERE ipaddress = '127.0.0.1'
AND datevoted
BETWEEN `2011-09-02 00:00:00` AND `2011-09-03 00:00:00`
LIMIT 0 , 30

That's why you lose any records that are between passed midnight and now.


Use NOW() :

SELECT *
FROM votelog
WHERE ipaddress = '127.0.0.1'
  AND datevoted
      BETWEEN DATE_SUB( NOW( ) , INTERVAL 24 HOUR )
          AND NOW( )
LIMIT 0 , 30

Useful readings:

MySQL docs: The DATETIME, DATE, and TIMESTAMP Types

MySQL docs: Date and Time functions

SO question: datetime vs timestamp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文