【问题标题】:How to join result sets without using union or "or" in where condition.?如何在不使用联合或“或”条件的情况下加入结果集?
【发布时间】:2020-05-16 21:05:07
【问题描述】:

我的工具在 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`

下面使用“或”运算符的组合查询

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

但我想要一个不使用“或”运算符和联合的查询

需要可能涉及连接或子查询的简单查询

【问题讨论】:

  • 无法在 WHERE 子句中处理 OR 的工具不可用。再买一个。
  • 如果您想要真正的 LEFT JOIN 结果,请将猴子条件移至 ON 子句。 (就像现在一样,您会得到常规的 INNER JOIN 结果。)
  • 工具生成SQL并在db中运行。但是设计成不能作为过滤条件部分的输入。有什么解决办法吗?
  • 不能使用 into 或配合 .it 的一种原始工具
  • “和联合”是什么意思?你知道select * from t where x or yselect * from t where x union select * from t where y 吗?你知道t left join u on x where u.c=... and ...t inner join u on x where u.c=... and ... 吗?您是否意识到 t left join u on xt inner join u on x union all 不匹配的 t 行由 null 扩展? x or y 也是 not (not x and not y)。请更具体地说明您的工具如何转换以及允许使用什么语法,或者您只是让人们在黑暗中猜测。 PS where 是一个带有“谓词”的“子句”。

标签: sql join sybase


【解决方案1】:

我认为不使用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'
                      )
        )

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

【讨论】:

    【解决方案2】:

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

        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      相关资源
      最近更新 更多