2015-09-23 60 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