我有一个SQLite数据库的短语及其原生翻译
code | language_id | script_source | script_target | transliteration
| | | |
yes | 1 | Yes. | Yes. | NULL
no | 1 | No. | No. | NULL
yes | 3 | Oui. | Oui. | NULL
no | 3 | Non. | Non. | NULL
其中language_id指定语言。我正在尝试编写一个SELECT
代码的查询,一种语言的script_source和另一种语言的script_target以及音译,所有在一行。我设法得到的最接近的是
SELECT code, script_source, transliteration FROM phrases WHERE language_id=001
UNION
SELECT code, script_target, transliteration FROM phrases WHERE language_id=003;
生成此查询结果:
code | script_source | transliteration
yes | Yes. | NULL
yes | Oui. | NULL
no | No. | NULL
no | Non. | NULL
这没关系,但是对于我的目的来说,如果`script_target'有它自己的专栏,像这样:
code | script_source | script_target | transliteration
yes | Yes. | Oui. | NULL
no | No. | Non. | NULL
我认为这可能需要某种JOIN
功能,但到目前为止我还没有达到预期效果。任何帮助将不胜感激。
编辑:我的错误,我希望音译对应script_target
值。
答案 0 :(得分:2)
一种方法是join
:
SELECT p1.code, p1.script_source,p2.script_source as script_target,
p2.transliteration as transliateration
FROM phrases p1 JOIN
phrases p2
ON p1.code = p2.code
WHERE p1.language_id = 1 AND p2.language_id = 3;
目前尚不清楚你想要哪种音译。
答案 1 :(得分:1)
对于一组固定的language_id
值,您可以执行以下操作:
SELECT code,
MAX(CASE WHEN language_id = 1 THEN script_source END) AS script_source,
MAX(CASE WHEN language_id = 3 THEN script_target END) AS script_target,
MAX(CASE WHEN language_id = 3 THEN transliteration END) AS transliteration
FROM phrases
WHERE language_id IN (1,3)
GROUP BY code
上述查询会选择与transliteration
对应的language_id = 3
值。
答案 2 :(得分:1)
您所描述的是一个联接: - )
SELECT
source.code,
source.script_source,
source.transliteration as source_transliteration,
target.script_taget,
target.transliteration as target_transliteration
FROM phrases source
JOIN phrases target ON target.code = source.code
WHERE source.language_id = 001
AND target.language_id = 003;