2016-09-29 60 views
1

此查询工作正常的MySQL,但不是在PostgreSQL的:查询工作正常的MySQL,但不是在PostgreSQL的

select 
concat(hc.id, hl.languagecode) AS id, 
hc.id AS category_id, 
hl.languagecode AS languagecode 
from language hl 
join category hc 
left join categorytranslation hct ON hl.languagecode = hct.languagecode and hc.id = hct.category_id; 

在PostgreSQL有时我得到的错误:

ERROR: syntax error at end of input LINE 9: ....languagecode = hct.languagecode and hc.id = hct.category_id ^Query failed PostgreSQL said: syntax error at end of input

还是空的结果。 我正在使用Postico来测试它。

它有什么问题?

我想要做的就是让这样的结果:

select 
     concat(hc.id, hl.languagecode) AS id, 
     hc.id AS category_id, 
     hl.languagecode AS languagecode, 
     hc.slug AS slug 
from category hc, language hl 

但我也想加入与翻译“categorytranslation”的表。如果翻译是可用的而不是'类别'表格。为此,我修改了这样的查询:

SELECT 
      concat(hp.id, hl.languagecode) AS id, 
      hp.id AS id, 
      hl.languagecode AS languagecode, 
      hpt.languagecode as translang, 
      CASE WHEN (hpt.languagecode is not null and hpt.slug is not null and trim(hpt.slug) <> '') 
       THEN hpt.slug 
       ELSE hp.slug 
      END AS slug, 
    from language hl 
join category hc 
left join categorytranslation hct ON hl.languagecode = hct.languagecode and hc.id = hct.category_id; 
+0

连接条件看起来有点奇怪,因为你正在做的'language'和'category'表之间的交叉连接。它是否正确? –

回答

0

在PostgreSQL中,每个连接都需要连接条件短语。在您的查询中,表格之间的关系似乎是hl -> hct -> hc;查询就变成了:

SELECT hc.id || languagecode AS id, 
     hc.id AS category_id, 
     languagecode 
FROM language hl 
LEFT JOIN hapis_hatracocategorytranslation hct USING (languagecode) 
JOIN category hc ON hc.id = hct.category_id; 

注意通过在第一连接条件使用USING句话有两个表的languagecode列之间没有歧义,所以你可以在选择列表中忽略的别名。

+0

谢谢你的回答。 它的工作原理,到目前为止,但结果返回结果只有一个“语言代码”。表'语言'包含5种语言。 这些字段是:id,active,languagecode – Gunnar

相关问题