我有一个这样的数据库结构:
Countries
CREATE TABLE IF NOT EXISTS `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_5D66EBAD77153098` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Countries Language
CREATE TABLE IF NOT EXISTS `country_languages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`language_id` int(11) DEFAULT NULL,
`country_id` int(11) DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_1532561982F1BAF4` (`language_id`),
KEY `IDX_15325619F92F3E70` (`country_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Language
CREATE TABLE IF NOT EXISTS `languages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`iso` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`is_primary` tinyint(1) DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_A0D153795E237E06` (`name`),
UNIQUE KEY `UNIQ_A0D1537961587F41` (`iso`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
以及语言表的数据:
id name iso
----------------------
1 English en
2 German de
3 Italian it
和国家表:
id code
----------------
1 ie
和国家语言表
id country_id language_id name
----------------------------------------------
1 1 1 Ireland in English
2 1 2 Ireland in German
我正在尝试编写一个将返回以下结果的查询,以便能够显示它。 (如果可能的话)
(Language) (Country name)
English Ireland in English
German Ireland in German
Italian #NULL OR EMPTY STRING
其次,我试图了解是否可以将英语设置为默认语言,以及当 id 为 3(意大利语)的语言内容不存在时,默认值应该回退,结果如下:
(Language) (Country name)
English Ireland in English
German Ireland in German
Italian Ireland in English #please note language id is 3 -> Italian.
最佳答案
好的,所以下面的查询可能不用子查询而是用连接来完成。我相信查询优化器会这样做,但我不太确定。
SELECT l.name as language,
(SELECT cl.name
FROM country_languages cl
WHERE cl.country_id=[the wanted country id]
ORDER BY cl.language_id=l.id DESC,
cl.language_id=1 DESC
LIMIT 1) as country_name
FROM languages l
在此版本中,language_id 1 用作首选后备,您可能可以以类似的方式添加更多语言。使用 FIND_IN_SET
作为二阶标准也可以(FIND_IN_SET(cl.language_id,'1,2,3') DESC
或您喜欢的任何顺序) .
当然,现在这个查询是针对一个固定的 country_id。对于具有另一个连接的多个国家,它可以以类似的方式扩展:
SELECT l.name as language,
(SELECT cl.name
FROM country_languages cl
WHERE cl.country_id=c.id
ORDER BY cl.language_id=l.id DESC,
cl.language_id=1 DESC
LIMIT 1) as country_name
FROM countries c
JOIN languages l
子查询的替代方法是两次加入 country_languages,然后只选择第一个不为空的(这可能是更简洁的解决方案之一):
SELECT l.name as language,
COALESCE(first.name, second.name) as country_name
FROM countries c
JOIN languages l
LEFT JOIN country_languages first ON
(first.country_id=c.id AND first.language_id=l.id)
LEFT JOIN country_languages second ON
(second.country_id=c.id AND second.language_id=1)
如果语言 id 1 是您的后备语言。这也可以扩展以提供多种后备语言......
关于php - 缺少语言回退的 mysql 翻译表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39228478/