在Oracle中的其他两个查询上进行SELECT

时间:2013-08-23 08:24:30

标签: sql oracle select

所以,我想说我想做点什么:

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(外部select语句)生成

a                   b


value 1            value 2

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

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

4 个答案:

答案 0 :(得分:0)

你基本上有你的解决方案。您只缺少查询的名称,所以请执行以下操作:

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

答案 1 :(得分:0)

目前尚不清楚如何从表中连接不同的行,但它可以是这样的:

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

答案 2 :(得分:0)

你的语法有点偏离SQL图表,但实质上是ritgh:

可以做一个子查询:

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;

也可以对其进行分类和分类,但在您的情况下则不需要。

答案 3 :(得分:0)

我认为这就是你要找的东西:

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