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 烹饪书》第二版

第 6 章 使用字符串 使用字符串

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

本章主要介绍 SQL 中的字符串操作。请记住,SQL 并不是为执行复杂的字符串操作而设计的,你可能(也将会)发现在 SQL 中处理字符串有时会很繁琐和令人沮丧。尽管 SQL 有其局限性,但不同的 DBMS 还是提供了一些有用的内置函数,我们也尝试以创造性的方式使用它们。本章尤其代表了我们在引言中试图传达的信息;SQL 是好的、坏的和丑陋的。希望你能从本章中更好地理解在使用字符串时,SQL 可以做什么,不可以做什么。在许多情况下,解析和转换字符串的简单程度会让你大吃一惊,而在另一些情况下,完成特定任务所需的 SQL 会让你大吃一惊。

除了 MySQL(只有replace )之外,本书所涉及的所有 DBMS 现在都提供了 TRANSLATE 和 REPLACE 函数。在最后一种情况下,值得注意的是,您可以通过使用嵌套的 REPLACE 函数来复制 TRANSLATE 的效果。

本章的第一个秘诀非常重要,因为后面的几个解决方案都要用到它。在很多情况下,你都希望能够通过一次移动一个字符的方式来遍历字符串。遗憾的是,SQL 不能轻松做到这一点。因为 SQL 的循环功能有限,所以需要模仿循环来遍历字符串。我们称这种操作为 "走字符串 "或 "走过字符串",第一个口诀解释了这种技术。这是使用 SQL 进行字符串解析的基本操作,本章中几乎所有配方都会引用和使用这一操作。我们强烈建议大家熟悉该技术的工作原理。

6.1 走绳

问题

您想遍历一个字符串,将每个字符作为一行返回,但 SQL 缺乏循环操作。例如,要将表 EMP 中的ENAME "KING "显示为四行,其中每行只包含 KING 中的字符。

解决方案

使用笛卡尔乘积生成在一行中返回字符串每个字符所需的行数。然后使用 DBMS 的内置字符串解析功能提取您感兴趣的字符(SQL Server 用户将使用 SUBSTRING 代替 SUBSTR,使用 DATALENGTH 代替 LENGTH):

1 select substr(e.ename,iter.pos,1) as C
2   from (select ename from emp where ename = 'KING') e,
3        (select id as pos from t10) iter
4  where iter.pos <= length(e.ename)


C
-
K
I
N
G

讨论

迭代字符串的关键在于连接一个有足够行数的表,以产生所需的迭代次数。本示例使用的表 T10 包含 10 行(其中有一列 ID,其值为 1 到 10)。该查询最多可返回 10 条记录。

下面的示例显示了 E 和 ITER 之间的笛卡尔乘积(即具体名称和 T10 中的 10 行之间的乘积),没有对ENAME 进行解析:

select ename, iter.pos
  from (select ename from emp where ename = 'KING') e,
       (select id as pos from t10) iter

ENAME             POS
---------- ----------
KING                1
KING                2
KING                3
KING                4
KING                5
KING                6
KING                7
KING                8
KING                9
KING               10

内联视图 E 的卡入度为 1,内联视图 ITER 的卡入度为 10。因此,笛卡尔乘积为 ...

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