此查询在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中我偶尔会遇到错误:
错误:输入结束时的语法错误LINE 9:.... languagecode = hct.languagecode和hc.id = hct.category_id ^查询失败PostgreSQL说:输入结束时的语法错误
或者是空的结果。 我正在使用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 :(得分: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
列之间不存在歧义,因此您可以省略选择列表中的别名。