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

第 3 章. 使用多个表格

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

本章将介绍如何使用连接和集合操作来组合多个表中的数据。连接是 SQL 的基础。集合操作也很重要。如果想掌握本书后面章节中的复杂查询,就必须从连接和集合操作开始。

3.1 将一个行集堆叠到另一个行集上

问题

您希望返回存储在多个表中的数据,从概念上讲就是将一个结果集堆叠在另一个结果集之上。这些表并不一定有共同的键,但它们的列具有相同的数据类型。例如,你想显示表 EMP 中部门 10 的员工姓名和部门编号,以及表 DEPT 中每个部门的姓名和部门编号。您希望结果集如下所示:

ENAME_AND_DNAME      DEPTNO
---------------  ----------
CLARK                    10
KING                     10
MILLER                   10
----------
ACCOUNTING               10
RESEARCH                 20
SALES                    30
OPERATIONS               40

解决方案

使用集合操作 UNION ALL 合并来自多个表的记录:

1  select ename as ename_and_dname, deptno
2    from emp
3   where deptno = 10
4   union all
5  select '----------', null
6    from t1
7   union all
8  select dname, deptno
9    from dept

讨论

UNION ALL 将来自多个记录源的记录合并为一个结果集。与所有集合操作一样,所有 SELECT 列表中的项目必须在数量和数据类型上匹配。例如,以下两个查询都将失败:

select deptno   |  select deptno, dname
  from dept     |    from dept
 union all      |   union all
select ename    |  select deptno
  from emp      |    from emp

值得注意的是,如果存在重复数据,UNION ALL 将包括重复数据。如果要过滤掉重复数据,请使用 UNION 操作符。例如,EMP.DEPTNO 和 DEPT.DEPTNO 之间的 UNION 只返回四条记录:

select deptno
  from emp
 union
select deptno
  from dept

   DEPTNO
---------
       10
       20
       30
       40

指定 "联合 "而不是 "联合全部 "很可能会导致排序操作,以消除重复。在处理大型结果集时,请记住这一点。使用 UNION 与下面的查询大致相同,后者将 DISTINCT 应用于 UNION ALL 的输出:

select distinct deptno
  from (
select deptno
  from emp
 union all
select deptno
  from dept
         )

   DEPTNO
---------
       10
       20
       30
       40

除非万不得已,否则不会在查询中使用 DISTINCT,同样的规则也适用于 UNION:除非万不得已,否则不要用它来代替 UNION ALL。例如,虽然在本书中我们出于教学目的限制了表的数量,但在实际生活中,如果你要查询一个表,可能有更合适的方法来查询单个表 。

3.2 合并相关行

问题

您想通过连接已知的公共列或连接共享公共值的列来返回多个表中的行。例如,您想显示部门 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