LEFT JOIN中的语法错误

时间:2014-09-15 07:25:03

标签: postgresql

我的查询如下:

Select 
    array_to_string(r.media,',') AS service_type,
               array_to_string(array_agg(distinct s.state_name), ',') as primary_location 
   FROM contract.contract c
   LEFT JOIN customer.customer_state s , contract.rights r ON s.id = ANY (r.state) 
    WHERE c.customer_code::text = 'YYYY'::text 
  group by r.state 
  order by c.contract_name asc;

我得到了:

  

错误:语法错误在或附近","第44行:LEFT JOIN   customer.customer_state s,contract.rights r O ...

请建议

1 个答案:

答案 0 :(得分:2)

你不能混合那样的显式和隐式连接。

FROM contract.contract c
LEFT JOIN customer.customer_state s , contract.rights r ON
                                  ^^^

如果您要加入INNER JOIN条款,则应使用明确的CROSS JOINLEFT JOIN条款。

联接的谓词必须始终紧跟联接,而不需要其他附加功能。

如果要在多个表上执行左连接,则必须链接左连接。

FROM contract.contract c
LEFT JOIN customer.customer_state s ON (...)
LEFT JOIN contract.rights r ON (...)

从现在开始猜测,因为原始查询的意图不明确。 也许你的意思是

FROM contract.contract c
INNER JOIN customer.customer_state s ON (...??...)
LEFT JOIN contract.rights r ON (s.id = ANY (r.state))

虽然我没有看到任何谓词将contract与原始版本中的rightscustomer_state相关联,因此很难猜出您的意图。如果你真的想要一个笛卡尔积(交叉连接)你会写:

FROM contract.contract c
CROSS JOIN customer.customer_state s
LEFT JOIN contract.rights r ON (s.id = ANY (r.state))