SQL基础:使用SQL语句实现查询排序,顺序和倒序
SQL语句查询排序,顺序和倒序
SELECT
Company, OrderNumber
FROM
Orders
ORDER BY
Company DESC, OrderNumber ASC
#ASC升序,DESC倒序
Company, OrderNumber
FROM
Orders
ORDER BY
Company DESC, OrderNumber ASC
#ASC升序,DESC倒序
SQL查询结果排序
1.以指定顺序返回查询结果(order by的asc为升序,desc为降序)
select ename,job,sal from emp
where deptno = 10
order by sal asc
where deptno = 10
order by sal asc
2.多字段排序
select empno,deptno,sal,ename,job
from emp
order by deptno,sal desc
from emp
order by deptno,sal desc
3.依据子串排序(SQL Sever使用substring(),其它用substr,而substr只有两个参数)
select ename,job
from emp
order by substring(job,len(job)-2,2)
from emp
order by substring(job,len(job)-2,2)
4.排序时对null值处理
select ename,job,comm
from emp
order by 3
from emp
order by 3
5.依据条件逻辑动态调整排序项
select ename,sal,job,comm
from emp
order by case when job = ‘SALESMAN’ then comm else sal end
from emp
order by case when job = ‘SALESMAN’ then comm else sal end
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。