2016-09-29 56 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

相關問題