[英]MySQL, select from different table… IF
I'm having a small trouble since it was a long time ago i studies databases and querys. 我有一个小麻烦,因为很久以前我研究数据库和查询。
For example i'll have two tables for cd:s, one with data and one with alternative translations. 例如,我将有两个用于cd:s的表,一个表包含数据,另一个表包含替代翻译。 In the CD-table i have the original language, and it looks something like this 在CD表中,我使用的是原始语言,看起来像这样
Table for CDs (cds):
id | name | language
-----------------------
1 | aaa | en
2 | bbb | en
3 | ccc | fi
Table for languages (languages):
cd_id | language | name
-----------------------
1 | fi | AAA
1 | de | AAACHTUNG
3 | en | CCC
Now, i want to get all these cd:s in for example german, if there's no translation made i want it to be in the original language... 现在,我想用德语获得所有这些cd:s,如果没有翻译,我希望它使用原始语言...
Edit: German = de 编辑:德语= de
Since there's only one german translation (on CD #1) i want cd 2 & 3 in thier original names.. English and finnish in this case... 因为只有一种德语翻译(在CD#1上),所以我想用其原名的CD 2和3。在这种情况下,英语和芬兰语...
How can i do this? 我怎样才能做到这一点?
Edit 2: In this case when i ask for 'de' i would get: 编辑2:在这种情况下,当我要求“ de”时,我会得到:
AAACHTUNG
bbb
ccc
If i ask for 'en' i would get 如果我要求“ en”,我会得到
aaa
bbb
CCC
And so on... 等等...
This should do the trick ( SQL Fiddle ): 这应该可以解决问题( SQL Fiddle ):
select coalesce(l.name, c.name)
from cds c
left join languages l on l.cd_id = c.id
and l.language = 'de';
select cds.id, IFNULL(languages.name, cds.name) as name
from cds
left join languages on cds.id = languages.cd_id and languages.language = 'de'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.