williamZanelli Posted August 9, 2009 Share Posted August 9, 2009 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 Link to comment https://forums.phpfreaks.com/topic/169506-querying-date_add-returns-strange-result/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 9, 2009 Share Posted August 9, 2009 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(). Link to comment https://forums.phpfreaks.com/topic/169506-querying-date_add-returns-strange-result/#findComment-894345 Share on other sites More sharing options...
mattal999 Posted August 9, 2009 Share Posted August 9, 2009 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 Link to comment https://forums.phpfreaks.com/topic/169506-querying-date_add-returns-strange-result/#findComment-894347 Share on other sites More sharing options...
williamZanelli Posted August 9, 2009 Author Share Posted August 9, 2009 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? Link to comment https://forums.phpfreaks.com/topic/169506-querying-date_add-returns-strange-result/#findComment-894351 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.