【发布时间】:2021-04-14 16:56:38
【问题描述】:
标题含糊,抱歉。
我正在尝试将客户端代码转换为 Mysql 5.7 中的“纯”sql(如果有任何区别 - 解决方案也只能适用于 MariaDB 10.1)。
我有一个“codes”表和“code_translations”表。我现在正在尝试为代码表中的每个代码输出“最佳可用翻译”。
如果有要求给我代码“A”和翻译语言“de”的翻译 - 尝试首先找到带有“de”的翻译。如果未找到 - 使用“en”的可用翻译(默认后备)。如果没有“en” - 取由当前排序确定的第一个可用翻译。
从小提琴:
CREATE TABLE codes (id SERIAL, code TEXT, country TEXT);
CREATE TABLE code_translations (id SERIAL, code_id INT, language TEXT, label TEXT);
INSERT INTO codes (id, code, country)
VALUES
(1, 'A', 'DE'),
(2, 'B', 'DE'),
(3, 'C', 'DE');
INSERT INTO code_translations (code_id, language, label)
VALUES
(1, 'de', 'A-de'),
(1, 'en', 'A-en'),
(1, 'fr', 'A-fr'),
(2, 'de', 'B-de'),
(2, 'en', 'B-en'),
(3, 'nl', 'C-nl'),
(3, 'ru', 'C-ru');
基本选择 - 只产生所有行:
SELECT c.code,ct.label
FROM codes c
JOIN code_translations ct ON c.id=ct.code_id
-- WHERE ct.language=de
-- WHERE ct.language=fr
-- WHERE ct.language=ru
期望的输出:
-- WHERE language = de
-- output should be
-- |A|A-de| -- de is the direct match
-- |B|B-de| -- de is the direct match
-- |C|C-nl| -- first match determined by ordering
-- WHERE language = fr
-- output should be
-- |A|A-fr| -- fr is the direct match
-- |B|B-en| -- no "fr" - take "en"
-- |C|C-nl| -- first match determined by ordering
-- WHERE language = ru
-- output should be
-- |A|A-en| -- no "ru" - take "en"
-- |B|B-en| -- no "ru" - take "en"
-- |C|C-ru| -- ru is the direct match
【问题讨论】: