服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Sql Server - 使用SQL语句实现查询排序,顺序和倒序

使用SQL语句实现查询排序,顺序和倒序

2022-09-21 15:23丨背水丨 Sql Server

这篇文章主要介绍了使用SQL语句实现查询排序、顺序和倒序,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

SQL语句查询排序,顺序和倒序

?
1
2
3
4
5
6
7
8
SELECT
    Company, OrderNumber
FROM
    Orders
ORDER BY
    Company DESC, OrderNumber ASC
 
#ASC升序,DESC倒序

SQL查询结果排序

1.以指定顺序返回查询结果(order by的asc为升序,desc为降序)

?
1
2
3
select ename,job,sal from emp
where deptno = 10
order by sal asc

2.多字段排序

?
1
2
3
select empno,deptno,sal,ename,job
from emp
order by deptno,sal desc

3.依据子串排序(SQL Sever使用substring(),其它用substr,而substr只有两个参数)

?
1
2
3
select ename,job
from emp
order by substring(job,len(job)-2,2)

4.排序时对null值处理

?
1
2
3
select ename,job,comm
from emp
order by 3

5.依据条件逻辑动态调整排序项

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/ZUFE_ZXh/article/details/86679050

延伸 · 阅读

精彩推荐