2011-12-04 41 views
0

我有三個表,比如說這些;有條件的sql連接查詢從每個表中獲取適當的列

Table1:id,type,text 
Table2:id,table1_id,col1,col2 
Table3:id,table1_id,colA,colB 

所以,我想寫一個類似於; select * from Table1 /*join here, dont know*/ where id =3 得到有條件的結果,table1。*和table2。*或table.3根據類型..

我該怎麼辦?

回答

-1
SELECT * FROM Table1 LEFT JOIN Table2 On Table2.table1_id = Table1.id WHERE Table1.id = 3 
+0

那麼在哪裏是table3和條件連接? –

0

一個解決方案是在存儲過程中使用if語句從適當的表中選擇,有條件地在類型上。東西沿線

if type = something 
    select from table2 ... 
else 
    select from table3 ... 
end if 
+0

我的意思是我想選擇table1數據,並按照類型與table2或table3合併 –

相關問題