は1

2011-02-03 2 views
1
1.select col1,col2 from table1 where condition1=<<value1>> 
UNION ALL 
select col1,col2 from table2 condition1=<<value2>>; 

2. `select col1 from table3;` 

3. I need to write the 3rd query where i need the output of col1,col2 from table1 based on the col1 (sort based on col1 from table3). 

に複数のクエリの出力を合成し、私は(MySQLで)単一のクエリにしたい、このようは1

create table as temp_table as 
select col1,col2 from table1 where condition1=<<value1>> 
UNION ALL 
select col1,col2 from table2 condition1=<<value2>>; 

SELECT t1.col1,t1.col2 
FROM temp_table t1 
LEFT OUTER JOIN table3 t2 ON t1.col2=t2.col2 
order by t2.col1; 

を行うことができます&は、一時テーブルを使用していません。

どのような考えですか?ありがとう。

答えて

3

サブ選択としてユニオン結果を使用できます。

+0

すべてのサンプル例はありますか?ありがとう。 – Sharpeye500

+0

(table2の条件1 = <>からCOL2 ALL 選択COL1をCOL1を選択し、TABLE1条件1 = <> UNIONからCOL2) LEFT OUTERはsub.col2 = T3 ON表3のT3をJOINサブ からの選択COL1、COL2 を選択します。 col2 t3.col1による注文; – Jan

2

subqueriesを参照してください。それらは、クエリが実行されている間だけ存在する一時テーブルのようなものです。

1
Select T.A, T.B from 

(select col1 as A,col2 as B from table1 where condition1=<<value1>> 
UNION ALL 
select col1,col2 from table2 condition1=<<value2>>) as T, 
table3 as T2 
where T.A = T2.col1 

order by t2.col1