注意:日期用date_format后就不是日期格式了,而是字符串格式,不能用来算时间差。 思路:①准备一张25年、26年的表,tb1;②再准备一张只有25年的表,tb2;③tb2左连tb1,连接条件是月份相当,年份差1。 with tb1 as ( select job, date_format(date,'%Y-%m') dt, sum(num) cnt from resume_info where year(date) in (2025,2026) group by job, dt) select tb2.job, tb2.dt, tb2.cnt, tb1.dt, tb1.cnt from (select job, dt, cnt from tb1 where dt like '2025%') as tb2 left join tb1 on tb2.job=tb1.job and left(tb2.dt,4)=left(tb1.dt,4)-1 and right(tb2.dt,2)=right(tb1.dt,2) order by tb2.dt desc, job desc