使用JOIN的SQLite SELECT来生成所有关系数据

时间:2012-12-13 19:36:30

标签: tsql android-sqlite

我有一个db模式,我对T-SQL不太熟悉,我知道,ORM毁了我。开发人员:)

这是架构。

表1 1 - 1至3表(表2,表3,表4)和表(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

我需要一个查询来检索所有Table1记录及其对应的关系数据,Table1,Table2,Table3及其关系数据到Table5。

任何帮助表示赞赏。 谢谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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