我有几张桌子,展览,语言,表格和问题。问题在他们的表格中有lang_id
和form_id
,展览在表格中有form_id
。我想选择链接到表单的语言。唯一知道的是展览ID。怎么做?
您可以使用已知的展览ID从展览表中获取form_id。然后,您可以从form_id
中选择问题,并使用该数据检查哪些语言与问题相关联。但是我该如何完成这个查询?
这是一个sqlfiddle:http://sqlfiddle.com/#!9/bb726
我得到的查询就是这个:
SELECT f.id, f.name
FROM app_languages f,
app_exhibition b,
app_vragen_translations v
WHERE f.id = b.form_id
AND b.id = 4
AND v.lang_id;
答案 0 :(得分:2)
我没有使用你的小提琴因为你很好地描述了这个问题,所以查询很简单。
您需要更好地构建表和列的名称。使用外观漂亮的模式,您描述的查询是这样的:
SELECT DISTINCT lang.id, lang.name
FROM exhibition
INNER JOIN form
ON exhibition.formId = form.id
INNER JOIN question
ON form.Id = question.formId
INNER JOIN translation
ON translation.formId = form.id
INNER JOIN lang
ON translation.langId = lang.id
WHERE exhibition.id = 4
以下是小提琴的查询:
SELECT DISTINCT app_languages.id, app_languages.name
FROM app_exhibition
INNER JOIN app_forms
ON app_exhibition.form_id = app_forms.id
INNER JOIN app_vragen
ON app_forms.id = app_vragen.form_id
INNER JOIN app_vragen_translations
ON app_vragen_translations.vraag_id = app_vragen.id
INNER JOIN app_languages
ON app_vragen_translations.lang_id = app_languages.id
WHERE app_exhibition.id = 4
结果app_exhibition.id = 4
id name
4 German
如果使用其他展览app_exhibition.id = 5
id name
7 Dutch
2 English
一些提示:
注意:exhibition
和question
引用了字段formId
,因此如果此表中不需要进一步的信息,则可以省略表form
上的连接:
SELECT DISTINCT app_languages.id, app_languages.name
FROM app_exhibition
INNER JOIN app_vragen
ON app_exhibition.form_id = app_vragen.form_id
INNER JOIN app_vragen_translations
ON app_vragen_translations.vraag_id = app_vragen.id
INNER JOIN app_languages
ON app_vragen_translations.lang_id = app_languages.id
WHERE app_exhibition.id = 4