知识点

  1. 建立两个表,使用岗位和月份进行连接
  2. 第一个表是2025年的表,按照岗位和月份进行分组,最后求和。第二个表是2026年的表格式相同,将两个进行连接。
  3. 相同月分就是使用right()最后两个数字相同。right(s,n)函数返回字符串s最右边的n个字符

代码

select r1.job, first_year_mon, first_year_cnt, second_year_mon, second_year_cnt 
from (
      select job, date_format(date,'%Y-%m') as first_year_mon, sum(num) as first_year_cnt
      from resume_info
      where date like '2025%'
      group by job, first_year_mon) as r1
join
      (select job, date_format(date,'%Y-%m') as second_year_mon, sum(num) as second_year_cnt
       from resume_info
       where date like '2026%'
       group by job,second_year_mon) as r2
on r1.job=r2.job 
and right(first_year_mon,2) = right(second_year_mon,2) 
order by first_year_mon desc, r1.job desc