【问题标题】:SELECT on two other queries in Oracle在 Oracle 中的其他两个查询上选择
【发布时间】:2013-08-23 16:39:51
【问题描述】:

所以,假设我想做这样的事情:

SELECT Query1.a, 
       Query2.b 
FROM   (
           SELECT q as a 
           FROM   somewhere
       ), 
       (   
           SELECT g as b 
           FROM   elsewhere
       )
where  Query 1 is 
       (
           SELECT q as a 
           FROM   somewhere
       ) 
       and Query2 is 
       (
           SELECT g as b 
           FROM   elsewhere
       )

所以,我想从另外两个选择语句中进行选择。

查询 1 生成一个表

a

value1

查询 2 生成一个表

b

value 2

查询 3(外部选择语句)产生

a                   b


value 1            value 2

因此,本质上,两个结果表合并为列而不是行。

谢谢你,如果你有任何提示。

【问题讨论】:

    标签: sql oracle select


    【解决方案1】:

    你基本上有你的解决方案。您只是缺少查询的名称,因此请这样做:

    SELECT Query1.a, 
           Query2.b 
    FROM   (
               SELECT q as a 
               FROM   somewhere
           ) Query1, 
           (   
               SELECT g as b 
               FROM   elsewhere
           ) Query2
    

    【讨论】:

      【解决方案2】:

      不清楚您需要如何连接表中的不同行,但可能是这样的:

      select query1.a,
             query2.b
      FROM
      (select q as a, ROW_NUMBER() OVER (ORDER BY q) as RN from a) Query1
      
      FULL JOIN 
      (select q as b, ROW_NUMBER() OVER (ORDER BY q) as RN from b) Query2
      ON Query1.RN=Query2.RN
      

      SQLFiddle example

      【讨论】:

        【解决方案3】:

        你的语法有点偏离 SQL 图表,但本质上是正确的:

        可以做一个子查询:

        select A.field from (select field from a_table) A;
        

        如果您想在 select 或 where 子句中使用查询,则必须为查询命名。

        甚至可以像常规表格一样组合它们:

        select A.field, B.other_field from (select field from table1) A, (select other_field from table2) B;
        

        也可以在其上进行各种位置、分组和排序,但在您的情况下不需要。

        【讨论】:

          【解决方案4】:

          我想这就是你要找的东西:

          SELECT query1.a, query2.b
          FROM 
            (SELECT q as a FROM somewhere) query1,
            (SELECT g as b FROM elsewhere) query2
          

          这是SQLFiddle to test the query

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-05-14
            • 1970-01-01
            • 2010-12-24
            • 1970-01-01
            • 1970-01-01
            • 2021-09-27
            • 2015-05-28
            • 1970-01-01
            相关资源
            最近更新 更多