SELECT t1.job, t1.year_mon AS first_year_mon, t1.cnt AS first_year_cnt, t2.year_mon AS second_year_mon, t2.cnt AS second_year_cnt FROM ( SELECT job, DATE_FORMAT (date, '%Y-%m') AS year_mon, MONTH(date) AS mon, SUM(num) AS cnt FROM resume_info WHERE YEAR (date) = '2025' GROUP BY job, year_mon, mon ) t1 JOIN( SELECT job, DATE_FORMAT (date, '%Y-%m') AS year_mon, MONTH(date) AS mon, SUM(num) AS cnt FROM resume_info WHERE YEAR (date) = '2026' GROUP BY job, year_mon, mon ) t2 WHERE t1.job = t2.job and t1.mon = t2.mon ORDER BY first_year_mon DESC, job DESC
分别查询出2025年和2026年需要的数据,再将这两个表使用内连接连起来,连接条件一是job,二是mon(月份)。需要注意的是,如果mon的连接条件改为MONTH(t1.year_mon) = MONTH(t2.year_mon),查询结果显示为空,搞不太清楚原因。难道是连接的条件中只能使用各表中已经存在的属性?