SQL从一个表web2py左侧连接两次

时间:2014-11-27 18:57:43

标签: python sql web2py

怎么做这件事? 我有两张桌子

表1:

-id
-table2_id_1
-table2_id_2

表2:

-id
-table3_id

表3:

-id 
-table4_id
-table5_id
-table6_id

表4,表5和表6:

-id
-name
-date

主表是Table1

db(db.Table1).select()

我需要加入两次Table2(colums),我需要加入Table3(在每个table2_id_1和table2_id_2字段table3_id是等于),而不是连接Table4,Table5,Table6

1 个答案:

答案 0 :(得分:0)

我不知道,如果我真的得到了,你想要做什么,但如果你只是想根据id加入表格,那样的东西应该有效:

SELECT *
FROM table1 a JOIN table2 b ON (a.table2_id_1 = b.id) JOIN
     table2 c ON (a.table2_id_2 = c.id) JOIN
     table3 d ON (b.table3_id = d.id) JOIN
     table3 e ON (c.table3_id = e.id) JOIN
     table4 f ON (d.table4_id = f.id) JOIN 
     table5 g ON (d.table5_id = g.id) JOIN
     table6 h ON (d.table6_id = h.id) JOIN
     table4 i ON (e.table4_id = i.id) JOIN 
     table5 j ON (e.table5_id = j.id) JOIN
     table6 k ON (e.table6_id = k.id)