2012-07-24 6 views
1

일부 MySQL 쿼리를 포스트 그레스로 이동하는 중입니다. 작동하지 않는이 쿼리를 실행했습니다.여러 테이블에 왼쪽 조인을 수행하는 데 문제가 있습니다.

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

문서를 검토 한 결과 올바른 것으로 보입니다. 여기 정확히 내가 뭘 잘못하고 있니? 모든 의견을 많이 주시면 감사하겠습니다.

답변

6

나는 postgres를 모르지만, 표준 SQL에서는 쉼표 구문 대신 일련의 LEFT JOIN 문이 필요합니다. 당신은 이것을 시작한 것 같았고 처음 두 개 이후에 멈추었습니다.

뭔가 같은 :

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

당신의 SQL에있는 다른 작은 오류의 몇 당신이 당신의 tp 별칭을 잊었다는 사실 같은있다 :

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

대안의 이전 SQL 구문은 귀하의 첫 번째 테이블, 그리고 합류 제약으로 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

에 마법처럼 일했다. 감사! –

1

왼쪽 당신이

가에 .... trip_author의 TA1 가입 왼쪽 테이블 당 하나의 합류 조인에 trip_person의 TP1에 가입 왼쪽 :

나는 이것이 당신이 후에 어떤 생각합니다. ... 는 ...에 trip_institution 가입

왼쪽 ... 그래서

관련 문제