Skip to Content
《Head First SQL》 (Chinese Edition), 2nd Edition
book

《Head First SQL》 (Chinese Edition), 2nd Edition

by Kimberly Fessel
June 2026
740 pages
13h 7m
Chinese
O'Reilly Media, Inc.
Content preview from 《Head First SQL》 (Chinese Edition), 2nd Edition

第 6 章. 高级 SELECT:排序与聚合

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

Image

是时候将你的 SELECT 语句提升到新的高度了。你已经知道如何使用 SELECT 语句查询数据以及如何使用 WHERE 子句。但有时,你的查询需要比仅靠 SELECT 和 WHERE 所能达到的更高层次。

在本章中,你将学习如何 对数据进行排序 。你将构建 数学计算 以及 聚合值万一你需要新的分类,你将学会如何运用“if-then-else”逻辑来创建自己的分类体系。来个SELECT跳跃,我们开始吧!

四处逛逛

自上一章结束以来,你的好友乔什一直忙得不可开交。

Image

以下是stores的一段数据。乔什只是按照他走访这些商家的顺序插入的行。

Image

ORDER (BY) 登场!

你说需要对查询结果进行排序?正好,你可以告诉 SQL 先SELECT某些数据,然后根据表中的某个列返回的数据进行 ORDER BY 排序

以下是乔什如何按字母顺序检索所有商家及其店铺类型的方法:

Image

这个ORDER BY子句意味着返回的行将按名称的字母顺序排列:

Image

ORDER BY 允许您按任意列对结果进行字母顺序排序。

ORDER 配合 WHERE

当然可以。

你可以使用WHERE子句过滤行,并用ORDER BY 对匹配结果进行排序。只需确保在WHERE 之前写完完整的ORDER子句即可。

Image
Image

有三家店铺满足WHERE ...

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

What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
How to Overcome a Power Deficit

How to Overcome a Power Deficit

Cyril Bouquet, Jean-Louis Barsoux
Relational Power Is the New Currency of Hybrid Work

Relational Power Is the New Currency of Hybrid Work

Lebene Soga, Yemisi Bolade-Ogunfodun, Nazrul Islam, Joseph Amankwah-Amoah

Publisher Resources

ISBN: 0642572408114