반응형
select
product_name,
ifnull(sum(if(date_format(order_date, '%Y') = '2015', cnt, 0)),0) as "2015",
ifnull(sum(if(date_format(order_date, '%Y') = '2016', cnt, 0)),0) as "2016"
from product a join product_order b
on a.product_id = b.product_id
group by product_name

==

select
product_name,
ifnull(sum(if(date_format(order_date, '%Y') = '2015', cnt, 0)),0) as "2015",
ifnull(sum(if(date_format(order_date, '%Y') = '2016', cnt, 0)),0) as "2016"
from product a, product_order b
where a.product_id = b.product_id
group by product_name

 

728x90

+ Recent posts