测试数据:
create temp table l (id integer,name text);
create temp table t (id integer);
create temp table t_i18n(id integer,l_id integer,t_id integer,name text);
insert into l(id, name) values
(1, 'lang_1'),
(2, 'lang_2');
insert into t(id) values(1);
insert into t_i18n(id, l_id, t_id, name) values
(1, 1, 1, 'Text in the language one'),
(2, 2, 1, 'Text in the language two');
执行此查询后:
select *
from t
inner join t_i18n i18n
on i18n.t_id = t.id;
我有这个结果:
id | id | l_id | t_id | name
----+----+------+------+--------------------------
1 | 1 | 1 | 1 | Text in the language one
1 | 2 | 2 | 1 | Text in the language two
是否可以修改上面的查询以获得下面的结果?
/*Expected result*/
id | name_lang_1 | name_lang_2
----+--------------------------+--------------------------
1 | Text in the language one | Text in the language two
最佳答案 通常交叉表()将是最快的.您需要在数据库中安装附加模块
tablefunc.
SELECT * FROM crosstab(
'SELECT t.id, l.name AS lang_name, i.name AS lang
FROM t
JOIN t_i18n i ON i.t_id = t.id
JOIN l ON l.id = i.l_id' -- could also just be "ORDER BY 1" here
,$$VALUES ('lang_1'::text), ('lang_2')$$)
AS l (id text, lang_1 text, lang_2 text);
如果您的案例实际上就像那样简单(几乎没有),使用CASE语句的查询会:
SELECT t.id
, min(CASE WHEN i.l_id = 1 THEN i.name ELSE NULL END) AS lang_1
, min(CASE WHEN i.l_id = 2 THEN i.name ELSE NULL END) AS lang_2
FROM t
LEFT JOIN t_i18n i ON i.t_id = t.id
LEFT JOIN l ON l.id = i.l_id
GROUP BY 1
ORDER BY 1;
此相关问题下两种解决方案的详细信息:
PostgreSQL Crosstab Query
由于每个SQL查询和每个函数都必须具有明确定义的返回类型,因此无法在单个查询中对动态数量的语言名称执行此操作.您可以编写一个函数来动态创建语句并在第二次调用中执行该语句.
还有一些多态类型的高级技术,我在这里写了一个全面的答案:
Dynamic alternative to pivot with CASE and GROUP BY
但是,上面简单的crosstab()查询适用于语言名称的超集.非存在语言的字段返回NULL.看看provided link.