[英]Can I replace Union all with any kind on joins?
I have the below query for negative testing, But I want to replace the union all
if possible.我有以下关于负面测试的查询,但如果可能的话,我想
union all
。
select A.*
from A
join B
on A.COL1=B.COL1
where B.COL3 is null
union all
select A.*
from A
join B
on A.COL2=B.COL4
where B.COL5 is null;
Need to get data from both SQL without using union all
需要在不使用
union all
的情况下从 SQL 获取数据
You could combine the two queries into a single join and collapse the where
condition into it:您可以将两个查询组合成一个连接并将
where
条件折叠到其中:
select A.*
from A
join B on (A.COL1 = B.COL1 and B.COL3 is null) or
(A.COL2 = B.COL4 and B.COL5 is null)
SELECT A.*
FROM A
JOIN B ON (A.COL1 = B.COL1 OR A.COL2 = B.COL4) AND B.COL3 IS NULL;
Since you're only after data from Table A you don't need the join to table B at all and can re-write this as an Exists...由于您只是在获取表 A 中的数据之后,您根本不需要连接到表 B,并且可以将其重写为 Exists ...
SELECT A.*
FROM A
WHERE EXISTS (SELECT 1
FROM B
WHERE A.COL1=B.COL1 and B.COL3 is null)
OR EXISTS (SELECT 1
FROM B
WHERE A.COL2=B.COL4 and B.COL5 is null)
But this has likely has two issues:但这可能有两个问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.