语法:
SELECT 列名1,列名2,列名n FROM 表名 [WHERE] ORDER BY 排序条件1,表达式,别名
案例:
# 按照学生成绩进行排序,从高到低。
SELECT * FROM Student ORDER BY score DESC;
# 按照学生成绩进行排序,从低到高(默认是升序)
SELECT * FROM Student ORDER BY score [ASC];
# 查询考试成绩大于90分,分数从低到高
SELECT * FROM Student WHERE score > 90 ORDER BY score;
# 查询员工信息,按照员工的年薪排序【按表达式排序】
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪 FROM Employees ORDER BY salary*12*(1+IFNULL(commission_pct,0));
# 上面的语句【按照别名排序】
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪 FROM Employees ORDER BY 年薪;
# 按照学生的姓名长度倒序排序
SELECT * FROM Student ORDER BY LENGTH(`name`) DESC;
# 多个排序条件,先按照姓名长度,再按照成绩排序
SELECT * FROM Student ORDER BY LENGTH(`name`) DESC,score;