如何在 where 条件下不使用联合或"or"来联接结果集。?



我有一个工具,它不能接受联合运算符或 where 函数中的 " 或 " 条件运算符

我有两个疑问

第一个查询

Select * from tableA A left outer join tableB B on A.a1=B.b1 where A.a1 =20200131 and B.b2= 'monkey'

第二个查询

Select * from tableA where A.a1 =20200131 and   A.a3 ='Tom`

使用下面的"or"运算符进行组合查询

Select * from tableA A left outer join tableB B on A.a1=B.b1 where B.b2= 'monkey' or A.a3 ='Tom'

但是我想要一个不使用"or"运算符和联合的查询

预计可能涉及联接或子查询的简单查询

尝试将两个结果集插入到新表中并选择该表

Select * into tableB from tableA A left outer join tableB B on A.a1=B.b1 where 
A.a1 
=20200131 and B.b2= 'monkey'
insert into tableB
Select * from tableA where A.a1 =20200131 and   A.a3 ='Tom'

select * from tableB

我认为不使用UNION/OR,将导致更复杂的查询。您可以考虑以下解决方案。

SELECT *
FROM tableA a
where a.a1=20200131
and (   exists (select null from tableB b
WHERE a.a1 = b.b1
and b.b2='monkey')
or exists (SELECT null FROM tableA c
WHERE a.a1 = c.a1
AND c.a3 = 'Tom'
)
)

或者,您可以创建临时表,然后插入两个查询的输出。

最新更新