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

第 1 章. 检索记录

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

本章主要介绍基本的 SELECT 语句。扎实掌握基础知识非常重要,因为这里涉及的许多主题不仅存在于更难的口诀中,而且在日常 SQL 中也能找到。

1.1 检索表中的所有行和列

问题

您有一个表格,想查看其中的所有数据。

解决方案

使用特殊 * 字符,并对表发出 SELECT:

1 select *
2   from emp

讨论

字符 * 在 SQL 中具有特殊含义。使用它将返回指定表的每一列。由于没有指定 WHERE 子句,因此也将返回每一行。另一种方法是单独列出每一列:

select empno,ename,job,sal,mgr,hiredate,comm,deptno
  from emp

在交互式执行的特别查询中,使用 SELECT * 更简单。不过,在编写程序代码时,最好单独指定每一列。性能是一样的,但通过明确指定,你总能知道从查询中返回哪些列。同样,这样的查询也更容易被其他人理解(他们可能知道也可能不知道查询中表的所有列)。 如果查询是在代码中进行的,而程序从查询中获得的列集与预期的不同,也会出现 SELECT * 的问题。至少,如果你指定了所有列,但缺少了一列或多列,那么任何错误都更有可能与缺少的特定列有关。

1.2 从表中检索行的子集

问题

您有一个表格,希望只查看满足特定条件的行。

解决方案

使用 WHERE 子句指定要保留的记录。例如,要查看分配给部门编号 10 的所有员工:

1 select *
2   from emp
3  where deptno = 10

讨论

通过 WHERE 子句,可以只检索感兴趣的记录。如果 WHERE 子句中的表达式对任何记录都为真,那么就会返回该记录。

大多数供应商都支持常用运算符,如 =、<、>、<=、>=、!和 <>。此外,你可能还需要满足多个条件的记录;这可以通过指定 AND、OR 和括号来实现,如下一条指令所示。

1.3 查找满足多个条件的行

问题

您希望返回满足多个条件的记录。

解决方案

使用 WHERE 子句以及 OR 和 AND 子句。例如,如果要查找部门 10 中的所有员工,以及赚取佣金的员工和部门 20 中赚取最多 2000 美元的员工:

1 select *
2   from emp
3  where deptno = 10
4     or comm is not null
5     or sal <= 2000 and deptno=20

讨论

可以使用 AND、OR 和括号的组合来返回满足多个条件的记录。在解决方案示例中,WHERE 子句查找满足以下条件的记录:

  • DEPTNO 为 10

  • COMM 不为 NULL

  • DEPTNO 20 的任何员工的工资为 2,000 美元或以下。

括号的存在会导致括号内的条件一起被评估。

例如,考虑一下如果在查询中加上括号,结果集会有什么变化:

select * from emp where ( deptno = 10 or comm is not null or sal <= 2000 ) and deptno=20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ ----- ----- ----------- ----- ---------- ------ 7369 SMITH CLERK 7902 17-DEC-1980 ...
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