0

我有一堆子查询,每个子查询都返回一堆带有两个 ID 字段的记录。我需要返回所有子查询中存在的所有 ID 对的列表。我在想我可以做这样的事情:

SELECT Q1.V1, Q1.V2
FROM ( [SUBQUERY1] ) AS Q1
INNER JOIN ( [SUBQUERY2] ) AS Q2 ON Q2.V1 = Q1.V1 AND Q2.V2 = Q1.V2
INNER JOIN ( [SUBQUERY3] ) AS Q3 ON Q3.V1 = Q2.V1 AND Q3.V2 = Q2.V2
INNER JOIN ( [SUBQUERY4] ) AS Q4 ON Q4.V1 = Q3.V1 AND Q4.V2 = Q3.V2

有没有更好的办法?

4

2 回答 2

0

显然答案是使用INTERSECT

[SUBQUERY1]
INTERSECT
[SUBQUERY2]
INTERSECT
[SUBQUERY3]
INTERSECT
[SUBQUERY4]

好的!

于 2013-05-07T04:39:58.087 回答
0

相交可能更好,但您也可以将 group by 与 HAVING COUNT(*) > number_of_subqueries 子句一起使用。

select V1, V2
from 
(
(subquery_1)
union 
(subquery_2)
union 
(subquery_3)
)
group by v1, v2
having count(*) > 3
于 2013-05-07T09:17:30.857 回答