lxndr Posted January 7, 2010 Share Posted January 7, 2010 I have a table that contains amongst other things the following columns: comment: text date: int(10) The date is a unix timestamp and what I'd like to be able to do is run a MySQL query to show the number of comments made each week or each month so that the output might be something like: January 2009: 345 February 2009: 384 etc. etc. I'm sure there's a way to do it with a single SQL query but have so far failed to get it working. Any help much appreciated. ___ Quote Link to comment https://forums.phpfreaks.com/topic/187607-grouping-by-month-or-week/ Share on other sites More sharing options...
kickstart Posted January 7, 2010 Share Posted January 7, 2010 Hi Try something like this SELECT FROM_UNIXTIME(datefield, '%Y %M'), COUNT(comment) FROM sometable GROUP BY UNIXTIME(datefield, '%Y %M') However be aware that you will need to play around to order it correctly (probably best done by bringing back the date column twice, once in a numeric format to order it by). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/187607-grouping-by-month-or-week/#findComment-990473 Share on other sites More sharing options...
lxndr Posted January 7, 2010 Author Share Posted January 7, 2010 Hi Try something like this SELECT FROM_UNIXTIME(datefield, '%Y %M'), COUNT(comment) FROM sometable GROUP BY UNIXTIME(datefield, '%Y %M') However be aware that you will need to play around to order it correctly (probably best done by bringing back the date column twice, once in a numeric format to order it by). All the best Keith Hi Keith, Many thanks for the suggestion but when I try that query I get an error message saying function UNIXTIME does not exist? Best wishes, Ian __ Quote Link to comment https://forums.phpfreaks.com/topic/187607-grouping-by-month-or-week/#findComment-990478 Share on other sites More sharing options...
kickstart Posted January 7, 2010 Share Posted January 7, 2010 Hi Err. Aliens abducted some of my typing. Well thats my excuse anyway. In the GROUP BY it should be FROM_UNIXTIME. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/187607-grouping-by-month-or-week/#findComment-990483 Share on other sites More sharing options...
lxndr Posted January 7, 2010 Author Share Posted January 7, 2010 Hi Err. Aliens abducted some of my typing. Well thats my excuse anyway. In the GROUP BY it should be FROM_UNIXTIME. All the best Keith Hehe, thanks, that did the trick, your help much appreciated! Best wishes, Ian Quote Link to comment https://forums.phpfreaks.com/topic/187607-grouping-by-month-or-week/#findComment-990493 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.