我想要实现不同年份,同一月份的数据相对应,把空行删掉,尝试使用了聚合函数max,和sum,也参考了https://stackoverflow.com/questions/57848916/eliminating-null-values-from-the-union-set-operators 这篇文章,发现只适合于单行,多行对多行就不太适用,请问有什么好办法么?

mysql合并 UNION ALL 行以删除 NULL 值
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- Mini厂程序员 2023-04-08 08:44关注
因为题主想要同月份的放到一行里面,这个实际上就是一个左连接,只需要加多一个虚拟的字段,这个字段的值需要把日期中的月份截取出来,然后通过左连接把月份对应上就好了,比如 select t1.xxx, t2.xxx from (select substring(yearmonth, 6, 2) as mon, t.* from table where 2023年数据 t) t1 left join (select substring(yearmonth, 6, 2) as mon, t.* from table where 2022年数据 t) t2 on t1.mon = t2.mon
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报