Sql 简明教程

SQL - EXCEPT

The SQL EXCEPT Operator

EXCEPT SQL 中的运算符用于从左操作数(查询)检索所有唯一记录,但右操作数(查询)的结果集中存在记录除外。

The EXCEPT operator in SQL is used to retrieve all the unique records from the left operand (query), except the records that are present in the result set of the right operand (query).

换句话说,该运算符将左侧查询的不同值与右侧查询的结果集进行比较。如果在右侧查询的结果集中找到左侧查询的值,就会将其从最终结果中排除。

In other words, this operator compares the distinct values of the left query with the result set of the right query. If a value from the left query is found in the result set of the right query, it is excluded from the final result.

为了更好地理解,请考虑下图所示记录的两张表 -

For better understanding consider two tables with records as shown in the following image −

exceptclause

如果我们对上述两张表执行 EXCEPT 运算符以检索名称,它将仅显示不在第二张表记录中且为第一张表中的不同记录。

If we perform the EXCEPT operator on the above two tables to retrieve the names, it will display the distinct records only from the first table which are not in common with the records of the second table.

这里,“Dev”在两张表中都是常见的。因此,EXCEPT 运算符将消除它,并仅检索“Sara”和“Jay”作为输出。

Here, "Dev" is common in both tables. So, the EXECPT operator will eliminate it and retrieves only "Sara" and "Jay" as output.

Syntax

以下是 Microsoft SQL Server 中 EXCEPT 运算符的 SQL 语法 -

Following is the SQL syntax of the EXCEPT operator in Microsoft SQL server −

SELECT column1, column2,..., columnN
FROM table1, table2,..., tableN
[Conditions] //optional
EXCEPT
SELECT column1, column2,..., columnN
FROM table1, table2,..., tableN
[Conditions] //optional

两个 SELECT 语句中列的数目和顺序应该是相同的。

The number and order of columns in both SELECT statements should be the same.

Example

首先,让我们使用以下查询创建一个名为 STUDENTS 的数据表−

First of all, let us create a table named STUDENTS using the following query −

CREATE TABLE STUDENTS(
   ID INT NOT NULL,
   NAME VARCHAR(20) NOT NULL,
   SUBJECT VARCHAR(20) NOT NULL,
   AGE INT NOT NULL,
   HOBBY VARCHAR(20) NOT NULL,
   PRIMARY KEY(ID)
);

使用以下查询将一些值插入到表中:

Let’s insert some values into the table using the following query −

INSERT INTO STUDENTS VALUES
(1, 'Naina', 'Maths', 24, 'Cricket'),
(2, 'Varun', 'Physics', 26, 'Football'),
(3, 'Dev', 'Maths', 23, 'Cricket'),
(4, 'Priya', 'Physics', 25, 'Cricket'),
(5, 'Aditya', 'Chemistry', 21, 'Cricket'),
(6, 'Kalyan', 'Maths', 30, 'Football'),
(7, 'Aditya', 'Chemistry', 21, 'Cricket'),
(8, 'Kalyan', 'Chemistry', 32, 'Cricket');

所生成的表如下所示:

The table produced is as shown below −

ID

NAME

SUBJECT

AGE

HOBBY

1

Naina

Mathematics

24

Cricket

2

Varun

Physics

26

Football

3

Dev

Mathematics

23

Cricket

4

Priya

Physics

25

Cricket

5

Aditya

Chemistry

21

Cricket

6

Kalyan

Mathematics

30

Football

7

Aditya

Chemistry

21

Cricket

8

Kalyan

Chemistry

32

Cricket

现在,让我们使用以下查询创建一个名为 STUDENTS_HOBBY 的另一个表:

Now, let us create another table named STUDENTS_HOBBY using the following query −

CREATE TABLE STUDENTS_HOBBY(
   ID INT NOT NULL,
   NAME VARCHAR(20) NOT NULL,
   HOBBY VARCHAR(20) NOT NULL,
   AGE INT NOT NULL,
   PRIMARY KEY(ID)
);

一旦创建了该表,让我们使用以下查询向该表中插入一些值:

Once the table is created, let us insert some values to the table using the query below −

INSERT INTO STUDENTS_HOBBY VALUES
(1, 'Vijay', 'Cricket', 18),
(2, 'Varun', 'Football', 26),
(3, 'Surya', 'Cricket', 19),
(4, 'Karthik', 'Cricket', 25),
(5, 'Sunny', 'Football', 26),
(6, 'Dev', 'Cricket', 23);

创建的表如下所示:

The table created is as follows −

ID

NAME

HOBBY

AGE

1

Vijay

Cricket

18

2

Varun

Football

26

3

Surya

Cricket

19

4

Karthik

Cricket

25

5

Sunny

Football

26

6

Dev

Cricket

23

现在,让我们对上述两张表执行 except 运算符 -

Now, let us perform the except operation on the above two tables −

SELECT NAME, HOBBY, AGE FROM STUDENTS
EXCEPT
SELECT NAME, HOBBY, AGE FROM STUDENTS_HOBBY;

Output

上述查询的输出如下所示:

Output of the above query is as shown below −

NAME

HOBBY

AGE

Aditya

Cricket

21

Kalyan

Cricket

32

Kalyan

Football

30

Naina

Cricket

24

Priya

Cricket

25

EXCEPT with BETWEEN Operator

我们可以在 SQL 中使用 EXCEPT 运算符和 BETWEEN 运算符,以排除落在指定范围内的记录。

We can use the EXCEPT operator with the BETWEEN operator in SQL to exclude records that fall within a specified range.

Example

在以下 SQL 查询中,我们正在从 STUDENTS 表中检索年龄在 20 至 30 岁之间的学生的记录,但不包括 ALSO 在 STUDENTS_HOBBY 表中年龄在 20 至 30 岁之间的学生 -

In the following SQL query, we are retrieving the records of students aged between 20 and 30 from the STUDENTS table, excluding those who are also aged between 20 and 30 from the STUDENTS_HOBBY table −

SELECT NAME, HOBBY, AGE
FROM STUDENTS
WHERE AGE BETWEEN 20 AND 30
EXCEPT
SELECT NAME, HOBBY, AGE
FROM STUDENTS_HOBBY
WHERE AGE BETWEEN 20 AND 30

Output

当我们执行程序查询时,将获得如下输出 -

When we execute the program query, the output is obtained as follows −

NAME

HOBBY

AGE

Aditya

Cricket

21

Kalyan

Football

30

Naina

Cricket

24

Priya

Cricket

25

Except with IN Operator

IN 运算符用于根据指定值列表过滤结果集。我们还可以在 SQL 中使用 EXCEPT 运算符和 IN 运算符,以排除与指定列表中的值匹配的记录。

The IN operator is used to filter a result set based on a list of specified values. We can also use the EXCEPT operator with the IN operator in SQL to exclude records that matches values in the specified list.

Example

这里,我们正在从 STUDENTS 表中检索爱好为 Cricket 的学生的记录,但不包括 ALSO 在 STUDENTS_HOBBY 表中爱好为 Cricket 的学生 -

Here, we are retrieving the records of students with Cricket as a hobby, from the STUDENTS table, excluding those who also have Cricket as hobby from the STUDENTS_HOBBY table −

SELECT NAME, HOBBY, AGE FROM STUDENTS
WHERE HOBBY IN('Cricket')
EXCEPT
SELECT NAME, HOBBY, AGE FROM STUDENTS_HOBBY
WHERE HOBBY IN('Cricket')

Output

以下是以上查询的输出:

Following is the output of the above query −

NAME

HOBBY

AGE

Aditya

Cricket

21

Kalyan

Cricket

32

Naina

Cricket

24

Priya

Cricket

25

EXCEPT with LIKE Operator

LIKE 运算符用于对字符串执行模式匹配。EXCEPT 运算符也可与 SQL 中的 LIKE 运算符一起使用,以排除与指定模式匹配的行。

The LIKE operator is used to perform pattern matching on a string. The EXCEPT operator can also be used with the LIKE operator in SQL to exclude rows that matches with the specified pattern.

Example

在这里,我们从 HOBBY 列的值以“F”开头的 STUDENTS 表中检索记录,同时排除 STUDENTS_HOBBY 表中的类似行−

In here, we are retrieving records from the STUDENTS table where the values in the HOBBY column starts with 'F', while excluding similar rows from the STUDENTS_HOBBY table −

SELECT ID, NAME, HOBBY, AGE FROM STUDENTS
WHERE HOBBY LIKE 'F%'
EXCEPT
SELECT ID, NAME, HOBBY, AGE FROM STUDENTS_HOBBY
WHERE HOBBY LIKE 'F%';

Output

以上查询的输出如下所示:

The output for the above query is produced as given below −

ID

NAME

HOBBY

AGE

6

Kalyan

Football

30