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

第 4 章. 更优的 WHERE 子句:筛选行

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

Image

现在正是深入探索的好机会。您已经尝试过使用 WHERE 子句进行简单的表行筛选,但现在您可以 对 WHERE 子句进行深入探索 ,了解它真正能做到什么。

在本章中,你将使用AND来查找同时满足两个或多个条件的行。若要查找满足多个条件中任意一个的行,则改用OR。你甚至将 发掘出新的运算符特性 ,助你寻找缺失的项目、数值范围或文本模式。

还在等什么?翻开下一页,让探索之旅开始吧。WHERE 将成为你新的得力助手。

身份错位

哎呀,艾比看起来不太高兴。让我们回去找她,看看发生了什么。

Image

我们可以使用带过滤条件的SELECT查询:

Image

为艾比查找美国境内的几个地点,这样她旅行时就无需携带护照了。以下是前几条结果:

多行查询需求

我们虽然找到了美国境内的地点,但艾比和她的朋友凯蒂并不需要飞往我们找到的所有地方。

如果我们尝试使用SELECT查询,但添加过滤条件要求必须乘坐飞机前往,会怎样呢?

Image

结果的前几行显示如下:

Image

组合查询

我们可以将两个搜索条件——即需要乘坐飞机才能到达的美国城市——通过关键字AND 组合成一个查询。最终的查询结果必须 同时满足这两个条件

Image

该查询实际上要求 SQL 返回两组结果:一组是 Abby 表中位于美国境内的所有地点,另一组是所有需要乘坐飞机才能到达的地点。随后,查询仅返回同时属于 这两个集合的城市。

使用 AND 满足两个条件

当表中的行需要满足两个(或更多)条件时,请在AND过滤器中使用关键字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