You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

不使用INTERSECT如何通过连接查询获取两个结果集共有的行

实现方案

你给出的示例中两个INTERSECT关联的查询都来自同一张Customers表,用自内连接就可以实现完全相同的效果,代码如下:

SELECT DISTINCT A.cust_name, A.cust_contact, A.cust_email
FROM Customers A
INNER JOIN Customers B
  ON A.cust_name = B.cust_name
  AND A.cust_contact = B.cust_contact
  AND A.cust_email = B.cust_email
WHERE A.cust_state IN ('IL','IN','MI')
  AND B.cust_name = 'Fun4All'
ORDER BY A.cust_name, A.cust_contact;

逻辑说明

  • 我们把Customers表分别取别名A、B,A对应INTERSECT左侧的查询结果,B对应INTERSECT右侧的查询结果
  • 内连接的条件设置为两个结果集返回的三个字段完全相等,本质就是匹配两个查询同时返回的行
  • 额外加DISTINCT是为了对齐INTERSECT的默认去重逻辑,如果你的表中没有完全重复的行,也可以省略
  • 如果你遇到的是跨两张表的INTERSECT场景,只要把上面的A、B换成对应的两张表,连接逻辑完全通用

补充:你这个单表场景其实更简单的写法是直接合并WHERE条件:SELECT cust_name, cust_contact, cust_email FROM Customers WHERE cust_state IN ('IL','IN','MI') AND cust_name = 'Fun4All' ORDER BY cust_name, cust_contact,执行效率比连接更高

内容的提问来源于stack exchange,提问作者Rakesh Poddar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 11:48:02