> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> EXCEPT 子句的文档，该子句仅返回第一个查询结果中不存在于第二个查询结果中的那些行。

# EXCEPT 子句

> `EXCEPT` 子句仅返回第一个查询结果中不存在于第二个查询结果中的那些行。

* 两个查询必须具有相同数量的列，且列的顺序和数据类型也必须相同。
* `EXCEPT` 的结果可能包含重复行。如果不希望出现这种情况，请使用 `EXCEPT DISTINCT`。
* 如果未指定括号，多个 `EXCEPT` 语句将按从左到右的顺序执行。
* `EXCEPT` 运算符的优先级与 `UNION` 子句相同，但低于 `INTERSECT` 子句。

<div id="syntax">
  ## 语法
</div>

```sql theme={null}
SELECT column1 [, column2 ]
FROM table1
[WHERE condition]

EXCEPT

SELECT column1 [, column2 ]
FROM table2
[WHERE condition]
```

条件可以根据你的需求写成任意表达式。

此外，`EXCEPT()` 还可像 BigQuery (Google Cloud) 一样，使用以下语法从同一表的结果中排除列：

```sql theme={null}
SELECT column1 [, column2 ] EXCEPT (column3 [, column4]) 
FROM table1 
[WHERE condition]
```

<div id="examples">
  ## 示例
</div>

本节示例展示了 `EXCEPT` 子句的用法。

<div id="filtering-numbers-using-the-except-clause">
  ### 使用 `EXCEPT` 子句过滤数字
</div>

下面是一个简单示例，返回 1 到 10 中 *不* 属于 3 到 8 的数字：

```sql title="Query" theme={null}
SELECT number
FROM numbers(1, 10)
EXCEPT
SELECT number
FROM numbers(3, 6)
```

```response title="Response" theme={null}
┌─number─┐
│      1 │
│      2 │
│      9 │
│     10 │
└────────┘
```

<div id="excluding-specific-columns-using-except">
  ### 使用 `EXCEPT()` 排除特定列
</div>

`EXCEPT()` 可用于快速从结果中排除列。例如，如果我们想从某个表中选取所有列，但排除其中少数几个特定列，如下例所示：

```sql title="Query" theme={null}
SHOW COLUMNS IN system.settings

SELECT * EXCEPT (default, alias_for, readonly, description)
FROM system.settings
LIMIT 5
```

```response title="Response" theme={null}
    ┌─field───────┬─type─────────────────────────────────────────────────────────────────────┬─null─┬─key─┬─default─┬─extra─┐
 1. │ alias_for   │ String                                                                   │ NO   │     │ ᴺᵁᴸᴸ    │       │
 2. │ changed     │ UInt8                                                                    │ NO   │     │ ᴺᵁᴸᴸ    │       │
 3. │ default     │ String                                                                   │ NO   │     │ ᴺᵁᴸᴸ    │       │
 4. │ description │ String                                                                   │ NO   │     │ ᴺᵁᴸᴸ    │       │
 5. │ is_obsolete │ UInt8                                                                    │ NO   │     │ ᴺᵁᴸᴸ    │       │
 6. │ max         │ Nullable(String)                                                         │ YES  │     │ ᴺᵁᴸᴸ    │       │
 7. │ min         │ Nullable(String)                                                         │ YES  │     │ ᴺᵁᴸᴸ    │       │
 8. │ name        │ String                                                                   │ NO   │     │ ᴺᵁᴸᴸ    │       │
 9. │ readonly    │ UInt8                                                                    │ NO   │     │ ᴺᵁᴸᴸ    │       │
10. │ tier        │ Enum8('Production' = 0, 'Obsolete' = 4, 'Experimental' = 8, 'Beta' = 12) │ NO   │     │ ᴺᵁᴸᴸ    │       │
11. │ type        │ String                                                                   │ NO   │     │ ᴺᵁᴸᴸ    │       │
12. │ value       │ String                                                                   │ NO   │     │ ᴺᵁᴸᴸ    │       │
    └─────────────┴──────────────────────────────────────────────────────────────────────────┴──────┴─────┴─────────┴───────┘

   ┌─name────────────────────┬─value──────┬─changed─┬─min──┬─max──┬─type────┬─is_obsolete─┬─tier───────┐
1. │ dialect                 │ clickhouse │       0 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ Dialect │           0 │ Production │
2. │ min_compress_block_size │ 65536      │       0 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ UInt64  │           0 │ Production │
3. │ max_compress_block_size │ 1048576    │       0 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ UInt64  │           0 │ Production │
4. │ max_block_size          │ 65409      │       0 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ UInt64  │           0 │ Production │
5. │ max_insert_block_size   │ 1048449    │       0 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ UInt64  │           0 │ Production │
   └─────────────────────────┴────────────┴─────────┴──────┴──────┴─────────┴─────────────┴────────────┘
```

<div id="using-except-and-intersect-with-cryptocurrency-data">
  ### 在加密货币数据中使用 `EXCEPT` 和 `INTERSECT`
</div>

`EXCEPT` 和 `INTERSECT` 往往可以通过不同的布尔逻辑来实现类似效果；如果你有两个共享同一列 (或多列) 的表，这两个运算符都很有用。
例如，假设我们有几百万行历史加密货币数据，其中包含交易价格和成交量：

```sql title="Query" theme={null}
CREATE TABLE crypto_prices
(
    trade_date Date,
    crypto_name String,
    volume Float32,
    price Float32,
    market_cap Float32,
    change_1_day Float32
)
ENGINE = MergeTree
PRIMARY KEY (crypto_name, trade_date);

INSERT INTO crypto_prices
   SELECT *
   FROM s3(
    'https://learn-clickhouse.s3.us-east-2.amazonaws.com/crypto_prices.csv',
    'CSVWithNames'
);

SELECT * FROM crypto_prices
WHERE crypto_name = 'Bitcoin'
ORDER BY trade_date DESC
LIMIT 10;
```

```response title="Response" theme={null}
┌─trade_date─┬─crypto_name─┬──────volume─┬────price─┬───market_cap─┬──change_1_day─┐
│ 2020-11-02 │ Bitcoin     │ 30771456000 │ 13550.49 │ 251119860000 │  -0.013585099 │
│ 2020-11-01 │ Bitcoin     │ 24453857000 │ 13737.11 │ 254569760000 │ -0.0031840964 │
│ 2020-10-31 │ Bitcoin     │ 30306464000 │ 13780.99 │ 255372070000 │   0.017308505 │
│ 2020-10-30 │ Bitcoin     │ 30581486000 │ 13546.52 │ 251018150000 │   0.008084608 │
│ 2020-10-29 │ Bitcoin     │ 56499500000 │ 13437.88 │ 248995320000 │   0.012552661 │
│ 2020-10-28 │ Bitcoin     │ 35867320000 │ 13271.29 │ 245899820000 │   -0.02804481 │
│ 2020-10-27 │ Bitcoin     │ 33749879000 │ 13654.22 │ 252985950000 │    0.04427984 │
│ 2020-10-26 │ Bitcoin     │ 29461459000 │ 13075.25 │ 242251000000 │  0.0033826586 │
│ 2020-10-25 │ Bitcoin     │ 24406921000 │ 13031.17 │ 241425220000 │ -0.0058658565 │
│ 2020-10-24 │ Bitcoin     │ 24542319000 │ 13108.06 │ 242839880000 │   0.013650347 │
└────────────┴─────────────┴─────────────┴──────────┴──────────────┴───────────────┘
```

现在假设我们有一张名为 `holdings` 的表，其中包含我们持有的加密货币列表以及对应的币数量：

```sql title="Query" theme={null}
CREATE TABLE holdings
(
    crypto_name String,
    quantity UInt64
)
ENGINE = MergeTree
PRIMARY KEY (crypto_name);

INSERT INTO holdings VALUES
   ('Bitcoin', 1000),
   ('Bitcoin', 200),
   ('Ethereum', 250),
   ('Ethereum', 5000),
   ('DOGEFI', 10),
   ('Bitcoin Diamond', 5000);
```

我们可以使用 `EXCEPT` 来回答这样的问题：**"我们持有的哪些币种从未跌到 10 美元以下？"**：

```sql title="Query" theme={null}
SELECT crypto_name FROM holdings
EXCEPT
SELECT crypto_name FROM crypto_prices
WHERE price < 10;
```

```response title="Response" theme={null}
┌─crypto_name─┐
│ Bitcoin     │
│ Bitcoin     │
└─────────────┘
```

这意味着，在我们持有的四种加密货币中，只有比特币从未跌破 10 美元 (基于本示例中提供的有限数据) 。

<div id="using-except-distinct">
  ### 使用 `EXCEPT DISTINCT`
</div>

请注意，在前一个查询结果中，出现了多条比特币持仓记录。你可以在 `EXCEPT` 后加上 `DISTINCT`，以去除结果中的重复行：

```sql title="Query" theme={null}
SELECT crypto_name FROM holdings
EXCEPT DISTINCT
SELECT crypto_name FROM crypto_prices
WHERE price < 10;
```

```response title="Response" theme={null}
┌─crypto_name─┐
│ Bitcoin     │
└─────────────┘
```

**另请参阅**

* [UNION](/zh/reference/statements/select/union)
* [INTERSECT](/zh/reference/statements/select/intersect)
