SQLite:结果集的内部连接

SQLite: Inner join on result set

在 SQLite 中,有没有办法在结果集上对某些 table 进行内部联接?

有点像这样假设查询:

SELECT el1,el2 FROM pairs
INNER JOIN (<some_query> AS result) ON el1 IN result AND el2 IN result;

多余的工作查询应该是:

SELECT el1,el2 FROM pairs
WHERE el1 IN (<some_query>) AND el2 IN (<same_query>);

其中 <some_query> 是:

SELECT el FROM set1 INNER JOIN set2 ON set1.el=set2.el;

显然,后者看起来可能很贵。

首选查询是什么?

some_query的逻辑可以用INTERSECT写得更高效:

SELECT el FROM set1
INTERSECT
SELECT el FROM set2

如果您对 some_query 使用 CTE,那么根据您的要求,您可以这样做:

WITH cte AS (
  SELECT el FROM set1
  INTERSECT
  SELECT el FROM set2
)
SELECT el1, el2 
FROM pairs
WHERE el1 IN cte AND el2 IN cte