介绍
在淘宝、京东等搜索商品时,我们通常会按照 销量 进行排序,在关系型数据库中通过 order by 进行排序。
如果不加
order by,按照默认排序,每个数据库的操作不同
语法
select * from 表名 order by 列名 [asc/desc]
解释:
- order by 一般放最后面
- asc 表示升序
- desc 表示降序
例子
不加 [asc/desc],默认 是升序
select * from emp order by sal;
asc升序
select * from emp order by sal asc;
desc降序
select * from emp order by sal desc;
多个排序
select * from emp order by sal ,comm desc;
第一排序是 sal,从小到大
第二排序是 comm,从大到小(当 sal值相同 时,才有效果)