你能帮忙找到解决方案吗?
我有这个查询
SELECT P.id, P.url, PT.name, PT.lang
FROM cms_pages P
INNER JOIN cms_pages_translations PT
ON PT.page_id = P.id
ORDER BY P.id DESC LIMIT 10;
返回类似这样的内容:
ID | URL | NAME | LANGUAGE
---------------------------------------------------
1 | 'hello-word' | 'Hello world' | 1
---------------------------------------------------
1 | 'hello-word' | 'Hola mundo' | 2
---------------------------------------------------
1 | 'hello-word' | 'Olá mundo' | 3
---------------------------------------------------
2 | 'now-online' | 'We're online' | 1
---------------------------------------------------
2 | 'now-online' | 'Estamos online' | 2
---------------------------------------------------
2 | 'now-online' | 'Estamos online' | 3
我怎样才能实现看起来像这样的东西:
ID | URL | NAME_1 | NAME_2 | NAME_3
-----------------------------------------------------------------------------
1 | 'hello-word' | 'Hello world' | 'Hola mundo' | 'Olá mundo'
-----------------------------------------------------------------------------
2 | 'now-online' | 'We're online' | 'Estamos online' | 'Estamos online'
-----------------------------------------------------------------------------
答案 0 :(得分:0)
如果您知道潜在语言的数量 - 这会导致知道额外列的数量 - 您可以使用conditional aggregation
。以下是基于样本数据的示例:
select id, url,
max(case when language = 1 then name end) name_1,
max(case when language = 2 then name end) name_2,
max(case when language = 3 then name end) name_3
from <yourquery>
group by id, url
如果您不知道语言数量,则必须使用dynamic sql
动态构建列。