954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Select query to results all 12 months even if data not exist

I have table names 'payroll' with following data

month , pay
January , 1200
March , 1500
December , 2000


I want the following result

Janury , 1200
February , 00
March , 1500
April , 00
May , 00
June , 00
July , 00
August , 00
September , 00
October , 00
November , 00
December , 00


Please help to make the query.

Thanks in advance

jazzyb
Newbie Poster
7 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Build a reference table with all month names and left join it to your data table.

smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 

nearly right

it's a left outer join

SELECT months.month, IFNULL( payroll.pay, 0 ) AS pay
FROM months
LEFT OUTER JOIN payroll ON payroll.month = months.month
drjohn
Posting Pro in Training
448 posts since Mar 2010
Reputation Points: 76
Solved Threads: 80
 

Please explain the difference between LEFT JOIN and LEFT OUTER JOIN in MySQL. To the best of my knowledge there isn't any.

smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 
Please explain the difference between LEFT JOIN and LEFT OUTER JOIN in MySQL. To the best of my knowledge there isn't any.


I can't find a clear explanation to this in docs but this question was asked on StackOverflow and the accepted answer was that it makes no difference which you use.

At another site someone wrote the following: "MySql only supports the LEFT OUTER JOIN syntax so as to support ODBC compliance."

d5e5
Practically a Posting Shark
812 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 
At another site someone wrote the following: "MySql only supports the LEFT OUTER JOIN syntax so as to support ODBC compliance."


Right

mwasif
Posting Whiz
315 posts since Dec 2007
Reputation Points: 29
Solved Threads: 48
 

You could use the following query as an alternative to creating a table of months.

SELECT m.month, p.pay
FROM (
SELECT 'January' AS
MONTH
UNION SELECT 'February' AS
MONTH
UNION SELECT 'March' AS
MONTH
UNION SELECT 'April' AS
MONTH
UNION SELECT 'May' AS
MONTH
UNION SELECT 'June' AS
MONTH
UNION SELECT 'July' AS
MONTH
UNION SELECT 'August' AS
MONTH
UNION SELECT 'September' AS
MONTH
UNION SELECT 'October' AS
MONTH
UNION SELECT 'November' AS
MONTH
UNION SELECT 'December' AS
MONTH
) AS m
LEFT JOIN payroll p ON m.month = p.month
d5e5
Practically a Posting Shark
812 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 
Please explain the difference between LEFT JOIN and LEFT OUTER JOIN in MySQL. To the best of my knowledge there isn't any.


Sorry, my mistake. I just had it drummed into me to say left outer join and forgot that left join was identical.

drjohn
Posting Pro in Training
448 posts since Mar 2010
Reputation Points: 76
Solved Threads: 80
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: