Jump to content

Querying DATE_ADD returns strange result


williamZanelli

Recommended Posts

:facewall:

 

ok guys, small problem,

 

I want to know how many of some product sold in the last 24hrs (1hr, 8hrs, etc. will be added later), so I run the following query

 

SELECT * FROM sales WHERE prod_category = 135 AND DATE_ADD(now(), INTERVAL 1 DAY)

 

This returns total sales to me, regardless of what "DATE_ADD(now(), INTERVAL 1 DAY)" is..? Any ideas what I've done worng?

 

Thanks

 

will

AND DATE_ADD(now(), INTERVAL 1 DAY)

 

DATE_ADD(anything except NULL) is always TRUE. Therefore WHERE prod_category = 135 AND TRUE will match everything with a prod_category of 135. You probably want to throw in a comparison between some column and the result of DATE_ADD().

SELECT * FROM sales WHERE prod_category = 135 AND DATE_ADD(now(), INTERVAL 1 DAY)

 

Should be:

 

SELECT * FROM sales WHERE prod_category = 135 AND DAY(TheSoldDate) = DAY(DATE_ADD(CURDATE(), INTERVAL 1 DAY))

 

I think.

 

EDIT: Look above me ^ for an explanation :)

Mattal

 

I tried the following

 

SELECT *
FROM sales
WHERE prod_category =135
AND DAY( sale_date ) = DAY( DATE_ADD( CURDATE( ) , INTERVAL 1
DAY )

 

MySQL Throws the following error - #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL....

 

Any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.