MySQL select with语句 [英] MySQL select with statements
问题描述
我正在学习更多的SQL,并遇到了问题",
I'm learning some more SQL, and ran into a "problem",
我有两个表格,例如下面的链接 http://www.sqlfiddle.com/#!2/403d4/1
I have two tabels like the link below http://www.sqlfiddle.com/#!2/403d4/1
由于我这个周末对我进行的所有SQL测试都相当迟钝,所以我问错了... 抱歉...
Since I'm quite retarded now from all SQL-test i have done this weekend, i asked really wrong... Sorry guys...
我真正想要的是
如果我有请求的语言,我想获取所有记录,并且如果请求的语言不在标题表中,我希望它说"或null ...
If i have the requested language, i want to get all the records, and if the requested language is not in the titles-table, i want it to say "" or null...
就像我要输入"de"中的所有记录一样,我想要:
like if i ask for all records in 'de', i want:
1 | ACHTUNG
2 | NULL
如果我要我想要的'en'中的所有记录
and if i ask for all the records in 'en' i want
1 | WARNING
2 | Ambulance
对这个错误的问题表示歉意.
Really sorry for the wrong question.
/* 我想做的事: 我有记录的ID和要求的语言. 如果所选语言不存在,我希望它采用其他语言.
/* What i want to do: I have the ID of a record and the requested language. If the selected language does not exsists, i want it to take the other language.
就像我有: 语言='de'并且id = 1 我要"ACHTUNG",
like if i have: language = 'de' and id = 1 I want 'ACHTUNG',
如果我有: 语言='de'和id = 2 我想要救护车",因为那里没有"de" ... */
if i have: language = 'de' and id = 2 i want "Ambulance" since there's no 'de'... */
我该怎么做?
推荐答案
http://www.sqlfiddle.com/#!2/403d4/89
SELECT rec.id, title.name
FROM Records rec
LEFT JOIN Titles title ON title.record_id = rec.id and title.language='de';
SELECT rec.id, title.name
FROM Records rec
LEFT JOIN Titles title ON title.record_id = rec.id and title.language='en';
ID NAME
1 ACHTUNG
2 (null)
ID NAME
1 Warning
2 Ambulance
这篇关于MySQL select with语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!