Populate PHP sales table from MySQL (inc. empty months) -


i have query, returns sales month:

select year(txn_date) 'year', monthname(txn_date) 'month', sum(grand_total) 'sales' transactions year(txn_date) between '$last_year' , '$this_year' group month 

what's way of populating table showing sales last 2 years, month, including months have no sales? sql as possible, in case don't think can me there's no month table or that.

am going have crude loop counts 12 , searches through array matches on month , year? feel i'm missing obvious, elegant way of doing this...

you can build in-line tables months / years using cross join:

select y.`year` 'year',  x.`month` 'month', sum(grand_total) 'sales' (    select 1 `month` union select 2 union select 3 union     select 4 union select 5 union select 6 union     select 7 union select 8 union select 9 union     select 10 union select 11 union select 12 ) x cross join (    select 2015 ` year` union select 2016) y left join transactions z on x.`month` =  month(txn_date) ,                                y.`year` = year(txn_date) group x.`month`, y.`year` 

demo here


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -