跳转到主要内容
INTERSECT 子句仅返回同时出现在第一个和第二个查询结果中的行。两个查询的列数、顺序和类型必须一致。INTERSECT 的结果可以包含重复行。 如果未指定括号,多个 INTERSECT 语句将按从左到右的顺序执行。INTERSECT 运算符的优先级高于 UNIONEXCEPT 子句。
SELECT column1 [, column2 ]
FROM table1
[WHERE condition]

INTERSECT

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

条件可以是根据你的需求设定的任意表达式。

示例

下面是一个简单示例,将 1 到 10 的数字与 3 到 8 的数字求交集:
Query
SELECT number FROM numbers(1,10) INTERSECT SELECT number FROM numbers(3,8);
Response
┌─number─┐
│      3 │
│      4 │
│      5 │
│      6 │
│      7 │
│      8 │
└────────┘
如果你有两个表共享某个共同的列 (或多列) ,INTERSECT 就很有用。只要两个查询的结果包含相同的列,就可以对其结果执行交集运算。例如,假设我们有数百万行历史加密货币数据,其中包含交易价格和交易量:
Query
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
┌─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 的表,其中包含我们持有的加密货币列表,以及每种货币的持有数量:
Query
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);
我们可以使用 INTERSECT 来回答这类问题:“我们持有的哪些加密货币曾以高于 100 美元的价格交易过?”
Query
SELECT crypto_name FROM holdings
INTERSECT
SELECT crypto_name FROM crypto_prices
WHERE price > 100
Response
┌─crypto_name─┐
│ Bitcoin     │
│ Bitcoin     │
│ Ethereum    │
│ Ethereum    │
└─────────────┘
这意味着,在某个时间点,比特币和以太坊的交易价格都曾超过 100 美元,而 DOGEFI 和 Bitcoin Diamond 的交易价格则从未超过 100 美元 (至少根据本示例中的数据来看是如此) 。

INTERSECT DISTINCT

请注意,在前面的查询中,有多条交易价格高于 100 美元的比特币和以太坊持仓记录。去掉重复行会更合适 (因为它们只是重复了我们已经知道的信息) 。你可以在 INTERSECT 后添加 DISTINCT,以消除结果中的重复行:
Query
SELECT crypto_name FROM holdings
INTERSECT DISTINCT
SELECT crypto_name FROM crypto_prices
WHERE price > 100;
Response
┌─crypto_name─┐
│ Bitcoin     │
│ Ethereum    │
└─────────────┘
另请参阅
最后修改于 2026年6月10日