Skip to Content
SQL 口袋指南,第 4 版
book

SQL 口袋指南,第 4 版

by Alice Zhao
May 2025
Intermediate to advanced
356 pages
2h 51m
Chinese
O'Reilly Media, Inc.
Content preview from SQL 口袋指南,第 4 版

第 8 章 高级查询概念 高级查询概念

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

除了第 4 章 "查询基础 "中介绍的六个主要子句和第 7 章 "操作符和函数 "中介绍的常用关键字外,本章还将介绍使用 SQL 查询处理数据的几种高级方法

表 8-1包括本章所涉及的四个概念的说明和代码示例。

表 8-1. 高级查询概念
概念 说明 代码示例

案例陈述

如果满足条件,则返回特定值。否则,返回另一个值。

SELECT house_id,
  CASE WHEN flg = 1
  THEN 'for sale'
  ELSE 'sold' END
FROM houses;

分组和总结

将数据分成若干组,汇总每组内的数据,并返回每组的值。

SELECT zip, AVG(ft)
FROM houses
GROUP BY zip;

Windows 功能

将数据分成若干组,在每组内对数据进行汇总或排序,并为每一行返回一个值。

SELECT zip,
  ROW_NUMBER() OVER
  (PARTITION BY zip
  ORDER BY price)
FROM houses;

转动和取消转动

将一列中的值转化为多列,或将多列合并为一列。受OracleSQL Server 支持。

-- Oracle syntax
SELECT *
FROM listing_info
PIVOT
  (COUNT(*) FOR
  room IN ('bd','br'));

本章将详细介绍表 8-1中的每个概念以及常见用例。

案例陈述

CASE 语句用于在查询中应用 if-else 逻辑。例如,可以使用CASE 语句来拼写值。 如果看到1 ,则显示vip 。否则,显示generaladmission

+--------+       +-------------------+
| ticket |       | ticket            |
+--------+       +-------------------+
|      1 |       | vip               |
|      0 |  -->  | general admission |
|      1 |       | vip               |
+--------+       +-------------------+

Oracle 中,您可能还会看到 DECODE函数,这是一个较早的函数,其操作类似于CASE 语句。

备注

使用CASE 语句可以在查询期间临时更新值。要保存更新的值,可以使用 UPDATE语句来保存更新后的值。

下面两节将介绍两种类型的CASE语句

  • 单列数据的简单 CASE 语句

  • 搜索 多列数据的CASE 语句

根据单列的If-Then 逻辑显示值

要检查单列数据的相等性,请使用简单的 CASE 语句语法。

我们的目标

不显示1/0/NULL 的值,而是显示vip/reserved seating/general admission 的值:

  • 如果flag = 1 ,那么ticket = vip

  • 如果flag = 0 ,那么ticket = reserved seating

  • 其他ticket = general admission

下面是一个示例表格:

SELECT * FROM concert; +-------+------+ | name | flag | +-------+------+ | anton | 1 | | julia | 0 | | maren | 1 | | ...
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

《MySQL 烹饪书》第 4 版

《MySQL 烹饪书》第 4 版

Sveta Smirnova, Alkin Tezuysal

Publisher Resources

ISBN: 9798341658042