7

我正在将一些 Mysql 查询移至 Postgresql,但我遇到了这个不起作用的查询。

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, ... 

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

4

2 回答 2

10

我不知道 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
于 2012-07-24T17:40:53.467 回答
2

您的左联接是您要加入的每个表一个

left join trip_author ta1 on ....
left join trip_person tp1 on ....
left join trip_institution on ...

...等等

于 2012-07-24T17:42:51.453 回答