sql语句执行问题,group by为什么能用select里用过函数生成的字段

date全是这样的日期2025-01-01

select job,date_format(date,'%Y-%m') as mon,sum(num) as cnt
from resume_info
where date>'2025-01-01' and date<'2025-12-31'
group by job,mon
order by mon desc,cnt desc

这个语句竟然能执行,不是group在select之前执行吗,那为什么select生成的mon字段在group by的时候就能被调用了呢,这个时候不是还没有mon字段吗?

你这里使用的是别名,这个sql会自动重写为下面这个样子再执行

select job,date_format(date,'%Y-%m') as mon,sum(num) as cnt
from resume_info
where date>'2025-01-01' and date<'2025-12-31'
group by job,date_format(date,'%Y-%m')
order by date_format(date,'%Y-%m') desc,cnt desc