Skip to Content
《SQL 烹饪书》第二版
book

《SQL 烹饪书》第二版

by Anthony Molinaro, Robert de Graaf
May 2025
Intermediate to advanced
570 pages
7h 38m
Chinese
O'Reilly Media, Inc.
Content preview from 《SQL 烹饪书》第二版

第 2 章 排序查询结果 对查询结果排序

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章的重点是自定义查询结果的外观。通过了解如何控制结果集的组织方式,可以提供更可读、更有意义的数据。

2.1 按指定顺序返回查询结果

问题

您想显示第 10 部门员工的姓名、职位和工资,并根据工资从低到高排序。您希望返回以下结果集:

ENAME       JOB               SAL
----------  ---------  ----------
MILLER      CLERK            1300
CLARK       MANAGER          2450
KING        PRESIDENT        5000

解决方案

使用 ORDER BY 子句:

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

讨论

通过 ORDER BY 子句,可以对结果集中的行进行排序。解决方案会根据 SAL 以升序对行排序。默认情况下,ORDER BY 将以升序排序,因此 ASC 子句是可选的。或者,指定 DESC 以降序排序:

select ename,job,sal
  from emp
 where deptno = 10
 order by sal desc

ENAME       JOB               SAL
----------  ---------  ----------
KING        PRESIDENT        5000
CLARK       MANAGER          2450
MILLER      CLERK            1300

您无需指定要排序的列的名称。您可以指定一个代表列的数字。数字从 1 开始,从左到右匹配 SELECT 列表中的项目。例如

select ename,job,sal
  from emp
 where deptno = 10
 order by 3 desc

ENAME       JOB               SAL
----------  ---------  ----------
KING        PRESIDENT        5000
CLARK       MANAGER          2450
MILLER      CLERK            1300

本例 ORDER BY 子句中的数字 3 与 SELECT 列表中的第三列 SAL 相对应。

2.2 按多个字段排序

问题

您想首先按 DEPTNO 升序,然后按工资降序对 EMP 中的记录进行排序。您希望返回以下结果集:

     EMPNO      DEPTNO         SAL  ENAME       JOB
----------  ----------  ----------  ----------  ---------
      7839          10        5000  KING        PRESIDENT
      7782          10        2450  CLARK       MANAGER
      7934          10        1300  MILLER      CLERK
      7788          20        3000  SCOTT       ANALYST
      7902          20        3000  FORD        ANALYST
      7566          20        2975  JONES       MANAGER
      7876          20        1100  ADAMS       CLERK
      7369          20         800  SMITH       CLERK
      7698          30        2850  BLAKE       MANAGER
      7499          30        1600  ALLEN       SALESMAN
      7844          30        1500  TURNER      SALESMAN
      7521          30        1250  WARD        SALESMAN
      7654          30        1250  MARTIN      SALESMAN
      7900          30         950  JAMES       CLERK

解决方案

在 ORDER BY 子句中列出不同的排序列,用逗号分隔: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

SQL Essentials For Dummies

SQL Essentials For Dummies

Richard Blum, Allen G. Taylor
Oracle SQL By Example

Oracle SQL By Example

Alice Rischert

Publisher Resources

ISBN: 9798341658813