有些时候需要需要对查询的语句进行排序,无论是升序还是降序,比如按成绩排序,按工资排序等等,使用MySQL数据库,总结一下order by用法
一、对单个列排序
排序有升序和降序两种,默认的是升序,以部门表为例
例如以部门编号升序为例
select * from departments order by dept_no;
等价于
select * from departments order by dept_no asc ;
效果为
以部门编号降序 ,关键字为 desc
select * from departments order by dept_no desc ;
二、多个列进行排序
部门员工表为例
以部门编号和入职日期排序语句为
select * from dept_emp order by dept_no,from_date;
以多个列进行排序时,会先按第一个字段进行排序,就比如例子中的部门编号dept_no,当部门编号一致时,在按照入职时间from_date排序,按多个列排序时,多个列之间要用逗号","隔开 ,升序使用asc关键字此关键字可以省略,降序时使用desc关键字,desc不能省略