2017-02-20 62 views
-2

假設我有多個查詢:將多個結果到1

SELECT col1, col2 FROM tab1; 
SELECT colA, colB FROM tab2; 
SELECT colTest, colBlah FROM tab3; 

每個查詢將返回唯一1條結果。

如何將這3個結果合併爲1?

結果將看起來像:

+------+------+------+------+---------+---------+ 
| col1 | col2 | colA | colB | colTest | colBlah | 
+------+------+------+------+---------+---------+ 
| abc | def | Ghi | JkL | 12A  | 42BD | 
+------+------+------+------+---------+---------+ 

回答

2

加入他們都:

SELECT t1.col1, t1.col2, t2.colA, t2.colB, t3.colTest, t3.colBlah 
FROM tab1 AS t1 
CROSS JOIN tab2 AS t2 
CROSS JOIN tab3 AS t3 
+0

@ user1170330 - (我知道你說的語句只返回一個記錄,但... )確保您瞭解[如何交叉連接工作​​](https://en.wikipedia.org/wiki/Join_(SQL)#Cross_join),當有*多於一條記錄時。 – Leigh

0
select tab1.col1, tab1.col2, tab2.colA, tab2.colB, tab3.colTest, tab3.colBlah from tab1, tab2, tab3