2015-09-23 66 views
1

我有2个表连接在一起,并返回我想要的东西有:case语句加入

SELECT a, b, table_one.c, d, e 
from table_one INNER JOIN table_two ON table_two.c=table_one.d 
WHERE table_two.c 

我的第三个表,Table_Three中,也有列c。我需要一个带有标志(如布尔值)的列,以确定该值是否出现在第三个表中。 我一直在尝试做一个case语句和左连接,但我无法弄清楚语法。

我知道我可以把如此,因为SELECT后另一列是这样的:

SELECT a, b, table_one.c, d, e 
    , case table_three.c 
      when table_three.other_column_in_table_three ISNULL THEN true   
      ELSE false 
     END 
from table_one INNER JOIN.... 

但我不知道这是非常正确的,我不知道如何添加一个左连接到这个查询。这是否正确?如果是这样,语法是如何工作的?

+0

您正在使用什么数据库管理系统? –

+0

我正在使用postgres – swinters

+0

案例表达式,不是case语句... – jarlh

回答

2

更简单的方法

select a, b, table_one.c, d, e 
    , table_three.other_column is not null as flag 
from table_one inner join table_two on table_one.d = table_two.c 
    left join table_three on -- put here the relation rules between table_three 
           -- and the other tables 
+0

因此对于表3之间的关系规则,我认为应该只是:table_three.c = table_one.d ---就像加入table_one和table_two 但我需要什么“和其他表”? – swinters

1

在MSSQL中,你可以尝试:

SELECT a, b, table_one.c, d, e, ISNULL(table_three.c, true) 
from table_one 
INNER JOIN table_two ON table_two.c = table_one.d 
LEFT JOIN table_three ON table_two.c = table_three.c