tags:

views:

26

answers:

1

Hi, I have to filter data for last 3 months from current date, so that would be to fetch data from Aug to Oct. But value exist for October only in mysql table, so now i want to display record in following format:

Month     Values
Aug          0
Sept         0
Oct          10

But my query only shows October month records as i dont have record for previous 2 months. How can i do this. Following is my query.

SELECT
CASE WHEN COUNT(DISTINCT(user_analytics_id)) > 0 THEN COUNT(DISTINCT(user_analytics_id)) ELSE 0 END as pic_views,
YEAR(user_profile_viewed_date) AS pic_viewed_year,
MONTHNAME(user_profile_viewed_date) as pic_viewed_month
FROM (`user_analytics`)
WHERE `user_id` = '1' AND `view_type` = 'picture'
AND `user_profile_viewed_date` BETWEEN '2010-07-28 04:23:56' AND '2010-10-28 04:23:56'
GROUP BY MONTH(user_profile_viewed_date) ORDER BY MONTH(user_profile_viewed_date) ASC

The above query is not working as i want it to. So pls help me on this..

A: 

Create another table with numbers 1 .. 12 and LEFT JOIN your table to it.

zerkms
cant we find a solution without using anyother table...?
Sudhir
@Sudhir: I never seen any other solution.
zerkms