mysql 这个查询怎么写

图片说明

3个回答

select year ,sum(case when month=1 then amount else 0 end) as m1
,sum(case when month=2 then amount else 0 end) as m2
,sum(case when month=3 then amount else 0 end) as m3
,sum(case when month=4 then amount else 0 end) as m4
from table
group by year

enjoyker
enjoyker 感谢~
2 年多之前 回复

select g1.year,g1.account as m1,g2.account as m2,g3.account as m3,g4.account as m4 from group_test g1
left join group_test g2 on g1.year = g2.year and g2.month = 2
left join group_test g3 on g1.year = g3.year and g3.month = 3
left join group_test g4 on g1.year = g4.year and g4.month = 4
where g1.month = 1

select g1.year, g1.account as m1,g2.account as m2,g3.account as m3,g4.account as m4 from group_test g1
left join group_test g2 on g1.year = g2.year
left join group_test g3 on g1.year = g3.year
left join group_test g4 on g1.year = g4.year
group by year

id_no_chinese
smallerCoder 不好意思,写错了。答案不对
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!