我有以下表格进行本地化:
Key | Value | lang
MainTitle | Welcome to my page | en
MainTitle | Bienvenue sur mon site | fr
....
并非所有键都已本地化,因此它目前的工作方式是。
这是我使用的查询:
SELECT * FROM local.strings s WHERE lang = 'en';
是否有一种非常有效的方法可以解决这个问题,而无需使用多个或内部查询?
答案 0 :(得分:2)
如果您想获得给定语言的所有密钥,请使用:
SELECT *
FROM local.strings s
WHERE lang = 'fr'
UNION ALL
SELECT *
FROM local.strings s
WHERE lang = 'en' AND
NOT EXISTS (SELECT 1 FROM local.strings s2 WHERE s2.key = s.key and s2.lang = 'fr');
这实际上与你的逻辑相反:
答案 1 :(得分:0)
SELECT `key`
, COALESCE(loc.value,en.value) value
, COALESCE(loc.lang,en.lang) lang
FROM strings en
LEFT
JOIN strings loc
ON loc.key = en.key
AND loc.lang <> en.lang -- or maybe "AND loc.lang = 'fr'"
WHERE en.lang = 'en';
答案 2 :(得分:0)
set @lang = "fr"; # You can write this DIRECTY in the query. This was just for testing
select * from (SELECT * FROM `test` ORDER BY CASE WHEN `lang` LIKE @lang THEN 1 ELSE 2 END) as tmp group by `key`;
测试表:
CREATE TABLE `test` (
`key` text,
`value` text,
`lang` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
测试数据:
MainTitle Welcome to my page en
MainTitle Bienvenue sur mon site fr
SubTitle Hello en
SubTitle 123 fr
Text1 Cookie en
Text1 Keks de
结果:
MainTitle Bienvenue sur mon site fr
SubTitle 123 fr
Text1 Cookie en