【发布时间】:2017-08-17 09:34:25
【问题描述】:
我有这本字典和带有翻译的表格。我可以在 SQLite 中使用它来做一个不错的选择
SELECT e.slug,
en.title,
en.locale
FROM entities AS e
LEFT JOIN (
locales AS en,
entity_locales AS el
) ON (
el.entity_id = e.id
AND el.locale_id = en.id
AND en.locale == 'en'
)
产生:
present, translation, en
missing, NULL, NULL
但我无法将其转换为 Postgres,因为我不明白当您在 SQLite 的 LEFT JOIN 中指定多个表时会发生什么:
SELECT e.slug,
en.title,
en.locale
FROM entities e
LEFT JOIN entity_locales el ON (el.entity_id = e.id)
JOIN locales en ON (
el.locale_id = en.id
AND en.locale = 'en'
)
只生产
present, translation, en
有没有办法让它工作?
SQLite 格式的数据库结构:
CREATE TABLE IF NOT EXISTS "entities" (
"id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
"slug" varchar
);
CREATE TABLE IF NOT EXISTS "entity_locales" (
"entity_id" integer,
"locale_id" integer
);
CREATE TABLE IF NOT EXISTS "locales" (
"id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
"title" varchar,
"locale" varchar
);
insert into entities(id, slug) values(1, 'present');
insert into entities(id, slug) values(2, 'missing');
insert into locales(id, title, locale) values(1, 'translation', 'en');
insert into entity_locales(entity_id, locale_id) values(1, 1);
【问题讨论】:
标签: sql postgresql sqlite join