php - MYSQL Left Join month count from two tables -


i want add columns represent month base counts other table.

i have 2 tables.

leave application

leaveid     userid       1            3 2            4 3            5 4            1 

leave dates

dateid    leaveid      leavedates  1            1          2015-10-06 2            1          2015-10-07  3            2          2015-11-01 4            2          2015-11-02 5            3          2015-01-01 6            4          2015-02-12 

i want end total leave count based on months:

userid    january     fabruary    march   on... 1           1           3            1 2           2           0            1 2           3           4            1 

do conditional summing of entries, this:-

select a.userid,         sum(if(month(leavedates) = 1, 1, 0)) january,         sum(if(month(leavedates) = 2, 1, 0)) february,         sum(if(month(leavedates) = 3, 1, 0)) march,         sum(if(month(leavedates) = 4, 1, 0)) april,         sum(if(month(leavedates) = 5, 1, 0)) may,         sum(if(month(leavedates) = 6, 1, 0)) june,         sum(if(month(leavedates) = 7, 1, 0)) july,         sum(if(month(leavedates) = 8, 1, 0)) august,         sum(if(month(leavedates) = 9, 1, 0)) september,         sum(if(month(leavedates) = 10, 1, 0)) october,         sum(if(month(leavedates) = 11, 1, 0)) november,         sum(if(month(leavedates) = 12, 1, 0)) december leave_application left outer join leave_dates b on a.leaveid = b.leaveid group a.userid 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -