2015-08-18 16 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

相关问题