select MM, sum(售价价*销售数量) as 售价收入, sum(成本价*销售数量) as 售价成本价, sum((售价价-成本价)*销售数量) as 利润 from ( select 售价价,成本价,销售数量,month(销售时间) as MM from table1 union all select 0,0,0,1 union all select 0,0,0,2 union all select 0,0,0,3 union all select 0,0,0,4 union all select 0,0,0,5 union all select 0,0,0,6 union all select 0,0,0,7 union all select 0,0,0,8 union all select 0,0,0,9 union all select 0,0,0,10 union all select 0,0,0,11 union all select 0,0,0,12 ) t group by MM
select ta.MM, sum(tb.售价价*tb.销售数量) as 售价收入, sum(tb.成本价*tb销售数量) as 售价成本价, sum((tb.售价价-tb.成本价)*tb.销售数量) as 利润 from tb left join ta on ta.id=tb.id group by mm
sum(售价价*销售数量) as 售价收入,
sum(成本价*销售数量) as 售价成本价,
sum((售价价-成本价)*销售数量) as 利润
from (
select 售价价,成本价,销售数量,month(销售时间) as MM
from table1
union all
select 0,0,0,1 union all
select 0,0,0,2 union all
select 0,0,0,3 union all
select 0,0,0,4 union all
select 0,0,0,5 union all
select 0,0,0,6 union all
select 0,0,0,7 union all
select 0,0,0,8 union all
select 0,0,0,9 union all
select 0,0,0,10 union all
select 0,0,0,11 union all
select 0,0,0,12
) t
group by MM
sum(tb.售价价*tb.销售数量) as 售价收入,
sum(tb.成本价*tb销售数量) as 售价成本价,
sum((tb.售价价-tb.成本价)*tb.销售数量) as 利润
from tb
left join ta on ta.id=tb.id
group by mm