2012-12-13 39 views
0

我有一個db模式,我對T-SQL還不是很熟悉,我知道,ORM毀了我。開發人員:)帶JOIN的SQLite SELECT導致所有關係數據

這是模式。表1 1 - 1到3表(Table2,Table3,Table4)和每個表(2,3,4)1 - 1到表5。

圖(TRY)表示:

Table1 1<->1 Table2 1<->1 Table5 
     1<->1 Table3 1<->1 Table5 
     1<->1 Table4 1<->1 Table5 

我需要一個查詢檢索所有表1記錄及其對應關係的數據,表1,表2,表3和他們的關係的數據表5。

任何幫助表示讚賞。 謝謝。

回答

0

我真正需要的是這樣的:

SELECT F.*, DE.Title as EnergyDrinkTitle, DE.[Picture] AS EnergyPicture, DI.Title AS InspDrinkTitle, DR.Title AS RelaxDrinkTitle 
FROM 
(Select M.*, E.DescriptionTitle, I.[DescriptionTitle], R.DescriptionTitle, 
E.[DrinkId] as EnergyDrink, I.DrinkId as InspDrink, R.[DrinkId] as RelaxDrink 
FROM [Member] M 
LEFT JOIN [Energy] E 
    ON M.[_id] = E.[MemberId] 
LEFT JOIN [Inspiration] I 
    ON M.[_id] = I.[MemberId] 
LEFT JOIN [Relax] R 
    ON M.[_id] = R.[MemberId] 
) AS F JOIN Drink DE ON F.EnergyDrink = DE._id 
JOIN Drink DI ON F.InspDrink = DI._id 
JOIN Drink DR ON F.RelaxDrink = DR._id 
0

沒有看到完整的表結構和列名,你應該能夠加入該表以類似的方式,因爲這:

select * 
from table1 t1 
left join table2 t2 
    on t1.id = t2.id 
left join table3 t3 
    on t1.id = t3.id 
left join table4 t4 
    on t1.id = t4.id 
left join table5 t5 
    on t2.id = t5.id 
    or t3.id = t5.id 
    or t4.id = t5.id 
+0

感謝名單會嘗試它,沒有太多的列,但需要所有。 –