我有两张桌子:
语言,作为我语言的索引。
isolanguage
"de"
"en"
"es"
"fi"
"fr"
"no"
"pl"
"ru"
翻译,我存储地方的翻译:
Name, translation, isolanguage
"Umeå Airport";"Flughafen Umeå";"de"
"Umeå Airport";"Umeå Airport";"en"
"Umeå Airport";"Aeropuerto de Umeå";"es"
"Umeå Airport";"Uumajan lentoasema";"fi"
我想要获得的是语言表中每种语言的翻译表中所有元素的列表。
预期结果:
"Umeå Airport";"Flughafen Umeå";"de"
"Umeå Airport";"Umeå Airport";"en"
"Umeå Airport";"Aeropuerto de Umeå";"es"
"Umeå Airport";"Uumajan lentoasema";"fi"
"Umeå Airport";NULL;"fr"
"Umeå Airport";NULL;"no"
"Umeå Airport";NULL;"pl"
"Umeå Airport";NULL;"ru"
关键是我需要所有语言,是否有翻译。
我正在尝试左连接查询,但无效,它只显示两个表中的元素(不包括NULLS)
select * from languages a
left join translations b
on (a.isolanguage = b.isolanguage)
"Umeå Airport";"Flughafen Umeå";"de"
"Umeå Airport";"Umeå Airport";"en"
"Umeå Airport";"Aeropuerto de Umeå";"es"
"Umeå Airport";"Uumajan lentoasema";"fi"
NULL;NULL;"fr"
NULL;NULL;"no"
NULL;NULL;"pl"
NULL;NULL;"ru"
答案 0 :(得分:1)
你可以使用
SELECT Name, translation, isolanguage
FROM
translations
WHERE
language.isolanguage=translation.isolanguage AND
language.isolanguage = 'de' OR
language.isolanguage = 'fr' OR .......
答案 1 :(得分:1)
您可以使用:
select name, isolanguage, translation
from (
select distinct t.name, l.isolanguage
from translations t
cross join languages l
) t
left join translations
using (name, isolanguage)
order by 1, 2;
name | isolanguage | translation
--------------+-------------+--------------------
Umea Airport | de | Flughafen Umea
Umea Airport | en | Umea Airport
Umea Airport | es | Aeropuerto de Umea
Umea Airport | fi | Uumajan lentoasema
Umea Airport | fr |
Umea Airport | no |
Umea Airport | pl |
Umea Airport | ru |
(8 rows)