我有一张表格来保存翻译。它的布局如下:
id | iso | token | content
-----------------------------------------------
1 | GB | test1 | Test translation 1 (English)
2 | GB | test2 | Test translation 2 (English)
3 | FR | test1 | Test translation 1 (French)
4 | FR | test2 | Test translation 2 (French)
// etc对于翻译管理工具,随着台去,我需要的东西更像是一个电子表格网格输出它:
token | GB | FR | (other languages) -->
-------------------------------------------------------------------------------------------
test1 | Test translation 1 (English) | Test translation 1 (French) |
test2 | Test translation 1 (French) | Test translation 2 (French) |
(other tokens) | | |
| | | |
| | | |
V | | |我认为这很容易,但事实证明,这比我预期的要困难得多!
经过大量的搜索和挖掘,我找到了group_concat,对于上面的具体情况,我可以开始工作并生成我期待的输出:
select
token,
group_concat(if (iso = 'FR', content, NULL)) as 'FR',
group_concat(if (iso = 'GB', content, NULL)) as 'GB'
from
translations
group by token;但是,这当然完全不灵活。它只适用于我迄今指定的两种语言。当我添加一种新语言时,我必须手动更新查询以将其考虑在内。
我需要上述查询的通用版本,这将能够生成正确的表输出,而不必知道存储在源表中的任何数据。
有些消息来源声称你不能在MySQL中轻松做到这一点,但我相信它一定是可能的。毕竟,这首先是数据库存在的事情。
有没有办法做到这一点?如果是这样,怎么样?