在ORACLE中相交(SQL不是PL/SQL)

在ORACLE中相交(SQL不是PL/SQL),sql,oracle,Sql,Oracle,我必须在两个select语句之间进行交集。条件是,只有当两个select语句都返回一些resultset时,才会发生交集。如果其中任何一个返回O行,则交集不起作用,并且最终结果必须是返回某些行的语句。这在SQL中可能吗(我不需要pl/SQL中的答案) 您可以使用这种构造来实现这一点 with t1 as (select level id from dual where 1 = 0 connect by level <= 3), t2 as (select level id fro

我必须在两个select语句之间进行交集。条件是,只有当两个select语句都返回一些resultset时,才会发生交集。如果其中任何一个返回O行,则交集不起作用,并且最终结果必须是返回某些行的语句。这在
SQL
中可能吗(我不需要
pl/SQL
中的答案)

您可以使用这种构造来实现这一点

with t1 as (select level id from dual where 1 = 0 connect by level <= 3),
     t2 as (select level id from dual where 1 = 1 connect by level <= 5)
select distinct * 
  from t1 
  where exists (select null from t2 where id = t1.id)
    or (select count(1) from t2) = 0
union all
select distinct * from t2 where (select count(1) from t1) = 0

很抱歉,刚才的评论。这是一个完美的解决方案,有很好的解释。这对我有用。非常感谢:-)
select * from t1 
intersect 
select * from t2 
union 
select * from t1 where (select count(1) from t2) = 0
union 
select * from t2 where (select count(1) from t1) = 0