mySQL failed attempt at getting posts from last 24hr, and last 60 min

僤鯓⒐⒋嵵緔 提交于 2019-12-12 10:19:26

问题


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.


回答1:


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.




回答2:


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)"; 



回答3:


$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'].




回答4:


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..




回答5:


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



来源:https://stackoverflow.com/questions/7292502/mysql-failed-attempt-at-getting-posts-from-last-24hr-and-last-60-min

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!