0

So, lets say I want to do something like:

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
       )

So, i want to select from two other select statements.

Query 1 produces a table

a

value1

Query 2 produces a table

b

value 2

And Query 3 (the outer select statement) produces

a                   b


value 1            value 2

So, essentially, two result tables are combined as columns and not as rows.

Thank you, if you have any hints.

4

4 回答 4

0

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

SELECT Query1.a, 
       Query2.b 
FROM   (
           SELECT q as a 
           FROM   somewhere
       ) Query1, 
       (   
           SELECT g as b 
           FROM   elsewhere
       ) Query2
于 2013-08-23T08:33:58.400 回答
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 示例

于 2013-08-23T08:35:26.597 回答
0

你的语法有点偏离 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;

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

于 2013-08-23T08:35:38.357 回答
0

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

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

这是一个用于测试查询的 SQLFiddle

于 2013-08-23T08:39:51.893 回答