2015-08-18 26 views
0

我有兩張桌子。我正在查詢一張桌子,並希望將結果與第二張桌子結合起來以獲得最終結果。如何擴展我的查詢以將結果與另一個表結合?

我的表是:

create table table1 (col1 int, col2 int) 
create table table2 (col3 int, col4 int) 

insert into table1 values 
(1, NULL), (2,10), (3, 20) 

insert into table2 values 
(1,100),(2,200),(3,300) 

查詢

SELECT col1 FROM table1 WHERE col2 IS NOT NULL 

給我

col1 
2 
3 

如何擴展我的查詢得到的結果如下:

col1 col4 
2  200 
3  300 

我把這個例子在SQL小提琴http://sqlfiddle.com/#!3/9e89e/1快速測試查詢。

回答

1
SELECT t1.col1,t2.col4 
FROM table1 t1 
join table2 t2 on 
t1.col1 = t2.col3 
WHERE t1.col2 IS NOT NULL 

您需要根據您的預期輸出連接表格。

Fiddle

相關問題