CROSS JOIN仅限某些AND条款

时间:2017-09-19 08:04:44

标签: sql sql-server stored-procedures

我遇到在查询中完成CROSS JOIN的情况。我现在想要向此结果集添加行,但这些额外的行不需要交叉连接。

示例:

SELECT 1,2,3
FROM tbl
INNER JOIN   [abc].[def].[ghi]
ON 1=1 -- this is the cross join
WHERE ((A BETWEEN B AND C) OR (A IS NULL AND D IN (
                                                   SELECT D 
                                                   FROM #TMP_table
                                                  )
                               )
       )

现在,我只想要来自WHERE CLAUSE第一部分的交叉连接结果集。 OR子句应该只添加到结果集中而不是CROSS JOINED。

这是可能的还是我尝试了不可能的事情?

提前致谢

1 个答案:

答案 0 :(得分:0)

SELECT 1,2,3
FROM tbl
CROSS JOIN   [abc].[def].[ghi]
WHERE A BETWEEN B AND C

UNION ALL

SELECT 1,2,3
FROM tbl
INNER JOIN   [abc].[def].[ghi]
ON ...
WHERE A IS NULL AND D IN 
(
    SELECT D 
    FROM #TMP_table
)