比如 select id,uid,name,score as s from student;
order by 后面可以用score 的别名s,因为order by 是根据select 的结果进行排序的。
group by 和where 就不能用score 的别名s,因为group by 和where 是条件,select 是结果,根据条件限制在去查询结果。
having 一般和group by 连用,用于分组后的统计,在做筛选。这时候不能用where
比如 select teacher_id, sum(score) as sum,avg(score) as avg from student group by teacher_id having avg>=60;
用于统计每个老师所带学生的平均成绩大于等于60分的记录。
版权声明:本文为aimee12345原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。