dachshund Posted January 16, 2014 Share Posted January 16, 2014 I have a piece of code which adds up all of the 'views' column from rows added in the past 1 day. I now need a piece of code which adds up all of the 'views' column from rows added the day before that. Any help would be great. Here's my current code. $sql = mysql_query("SELECT SUM(views) AS sum FROM table_name WHERE `date` >= SUBDATE(CURRENT_DATE, 1)"); Quote Link to comment https://forums.phpfreaks.com/topic/285417-date-minus-one-day/ Share on other sites More sharing options...
requinix Posted January 16, 2014 Share Posted January 16, 2014 What you have now looks at things that happened after a day ago. What you need is to look at things that happened after two days ago and before one day ago. Convert that pretty much literally into SQL. Quote Link to comment https://forums.phpfreaks.com/topic/285417-date-minus-one-day/#findComment-1465438 Share on other sites More sharing options...
davidannis Posted January 16, 2014 Share Posted January 16, 2014 (edited) I think what you have actually looks at yesterday and today. You can look at yesterday only with $sql = mysql_query("SELECT SUM(views) AS sum FROM table_name WHERE `date` = SUBDATE(CURRENT_DATE, 1)"); or the day before that with $sql = mysql_query("SELECT SUM(views) AS sum FROM table_name WHERE `date` = SUBDATE(CURRENT_DATE, 2)"); Edited January 16, 2014 by davidannis Quote Link to comment https://forums.phpfreaks.com/topic/285417-date-minus-one-day/#findComment-1465441 Share on other sites More sharing options...
Solution dachshund Posted January 16, 2014 Author Solution Share Posted January 16, 2014 ok i worked it out, it's: $sql = mysql_query("SELECT SUM(views) AS sum FROM table WHERE `date` >= SUBDATE(CURRENT_DATE, 2) AND `date` <= SUBDATE(CURRENT_DATE, 0)"); Quote Link to comment https://forums.phpfreaks.com/topic/285417-date-minus-one-day/#findComment-1465470 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.