Ihsaan Posted August 28, 2012 Share Posted August 28, 2012 I have a mySql table that contains the following columns: id date_created total ================== 1 2009-07-20 620 2 2009-03-25 320 How would I display all sales for the month of July in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/267691-how-would-i-calculate-sales-by-month/ Share on other sites More sharing options...
Psycho Posted August 28, 2012 Share Posted August 28, 2012 I assume you really mean all the sales for the month of July in 2012 (or some particular year), correct? If you had data over multiple years you wouldn't want the sales total for July from all years. So, you need to extract the month and year from the date field and GROUP BY those two values (if getting the totals for all year/month combos: SELECT SUM(total), MONTH(date_created) as month, YEAR(date_created) as YEAR FROM table_name GROUP BY year, month Quote Link to comment https://forums.phpfreaks.com/topic/267691-how-would-i-calculate-sales-by-month/#findComment-1373162 Share on other sites More sharing options...
Psycho Posted August 28, 2012 Share Posted August 28, 2012 I just reread your request and you want the total for ONE particular month. In that case you can just do this: SELECT SUM( win ) FROM table_name WHERE MONTH( `date_created` ) = 7 AND YEAR( `date_created` ) = 2012 Quote Link to comment https://forums.phpfreaks.com/topic/267691-how-would-i-calculate-sales-by-month/#findComment-1373166 Share on other sites More sharing options...
Ihsaan Posted August 28, 2012 Author Share Posted August 28, 2012 Thanks so much. I however would want the final php outcome to look something like this Month Total ============== July 12 1000 Aug 12 850 etc... Quote Link to comment https://forums.phpfreaks.com/topic/267691-how-would-i-calculate-sales-by-month/#findComment-1373173 Share on other sites More sharing options...
jazzman1 Posted August 28, 2012 Share Posted August 28, 2012 Thanks so much. I however would want the final php outcome to look something like this Month Total ============== July 12 1000 Aug 12 850 etc... I'm 100% sure that you don't have an idea what you're doing, right? Quote Link to comment https://forums.phpfreaks.com/topic/267691-how-would-i-calculate-sales-by-month/#findComment-1373175 Share on other sites More sharing options...
Psycho Posted August 28, 2012 Share Posted August 28, 2012 Thanks so much. I however would want the final php outcome to look something like this Month Total ============== July 12 1000 Aug 12 850 etc... OK, how does the first solution I provided not work for that? Quote Link to comment https://forums.phpfreaks.com/topic/267691-how-would-i-calculate-sales-by-month/#findComment-1373184 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.