使用SQL語句實現查詢排序,順序和倒序

SQL語句查詢排序,順序和倒序

SELECT 
	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

2.多字段排序

select empno,deptno,sal,ename,job
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)

4.排序時對null值處理

select ename,job,comm
from emp
order by 3

5.依據條件邏輯動態調整排序項

select ename,sal,job,comm
from emp
order by case when job = ‘SALESMAN' then comm else sal end

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: