在多个表上执行左连接时遇到问题

问题描述:

我正在将一些mysql查询转移到postgres,并且遇到了这个不起作用的过程。在多个表上执行左连接时遇到问题

select (tons of stuff) 
from trip_publication 

left join trip_collection AS "tc" on 
tc.id = tp.collection_id 

left join 
      trip_author ta1, (dies here) 
      trip_person tp1, 
      trip_institution tai1, 
      trip_location tail1, 
      trip_rank tr1 
    ON 
      tp.id = ta1.publication_id 
      AND tp1.id = ta1.person_id 
      AND ta1.order = 1 
      AND tai1.id = ta1.institution_id 
      AND tail1.id = tai1.location_id 
      AND ta1.rank_id = tr1.id 

该查询似乎在“trip_author ta1”行上死去,我在上面标记了它。实际的错误信息是:

syntax error at or near "," 
    LINE 77: (trip_author ta1, trip_person tp1, ... 

我经历了文档,它似乎是正确的。我在这里做错了什么?任何反馈将不胜感激。

我不知道postgres,但在常规SQL中,您需要一系列LEFT JOIN语句而不是您的逗号语法。你似乎已经开始这个,然后在前两个之后停止。

喜欢的东西:

SELECT * FROM 
table1 
LEFT JOIN table2 ON match1 
LEFT JOIN table3 ON match2 
WHERE otherFilters 

另一种方法是较旧的SQL语法:

SELECT cols 
FROM table1, table2, table3 
WHERE match AND match2 AND otherFilters 

有一个在你的SQL其他几个小错误,如您忘记您tp别名的事实您的第一张表格,并试图将where子句(ta1.order = 1)作为连接约束。

我想这是你所追求的:

select (tons of stuff) 
from trip_publication tp 
left join trip_collection AS "tc" on tc.id = tp.collection_id 
left join trip_author ta1 on ta1.publication_id = tp.id 
left join trip_person tp1 on tp1.id = ta1.person_id 
left join trip_institution tai1 on tai1.id = ta1.institution_id 
left join trip_location tail1 on tail1.id = tai1.location_id 
left join trip_rank tr1 on tr1.id = ta1.rank_id 
where ta1.order = 1 
+0

工作就像一个魅力。谢谢! – 2012-07-30 19:00:35

你的左连接是每桌一个要加入

留下.... 加入trip_author TA1左连接上trip_person TP1。 ... 左连接上trip_institution ...

...等等