所以我有一个相当复杂的数据库,我正在做一个学校项目,我们正在开发一个应用程序,它应该根据问题的类型在不同的图表中呈现答案。现在我正在使用以下代码,但这给了我三个表中的所有内容,问题是两个表都有一个名为“type”的列。它不是相同的“类型”,我想只访问“module_question”中的“类型”,因为那是告诉我它是什么类型的问题(例如,基于选择的问题,自由文本答案等)
SELECT * FROM module_question AS mq
INNER JOIN course_module AS cm ON mq.course_module_id = cm.course_module_id
INNER JOIN course_event AS ce ON cm.course_id = ce.course_id
WHERE ce.course_event_id = 223
and help_text IS NOT NULL
AND help_text <> '';
但如果我尝试这样做:
SELECT *, type AS question_type FROM module_question AS mq
INNER JOIN course_module AS cm ON mq.course_module_id = cm.course_module_id
INNER JOIN course_event AS ce ON cm.course_id = ce.course_id
WHERE ce.course_event_id = 223
and help_text IS NOT NULL
AND help_text <> '';
它给了我
列'类型'在字段列表
中不明确
是一个错误。 “course_module”和“module_question”都有一个名为“type”的列。
执行此查询的“正确”方法是什么?我希望能够从我的JSapplication中的module_question访问“type”。
答案 0 :(得分:0)
您必须定义要从哪个表中选择的类型。例如:
SELECT *, mq.type AS question_type FROM module_question AS mq ...
答案 1 :(得分:0)
仅更改mq.type而不是类型。
SELECT *, mq.type AS question_type FROM module_question AS mq
INNER JOIN course_module AS cm ON mq.course_module_id = cm.course_module_id
INNER JOIN course_event AS ce ON cm.course_id = ce.course_id
WHERE ce.course_event_id = 223
and help_text IS NOT NULL
AND help_text <> '';
答案 2 :(得分:-1)
您需要:
SELECT *, mq.type AS question_type FROM module_question AS mq
INNER JOIN course_module AS cm ON mq.course_module_id = cm.course_module_id
INNER JOIN course_event AS ce ON cm.course_id = ce.course_id
WHERE ce.course_event_id = 223
and help_text IS NOT NULL
AND help_text <> '';
OR
SELECT *, cm.type AS question_type FROM module_question AS mq
INNER JOIN course_module AS cm ON mq.course_module_id = cm.course_module_id
INNER JOIN course_event AS ce ON cm.course_id = ce.course_id
WHERE ce.course_event_id = 223
and help_text IS NOT NULL
AND help_text <> '';
OR
SELECT *, ce.type AS question_type FROM module_question AS mq
INNER JOIN course_module AS cm ON mq.course_module_id = cm.course_module_id
INNER JOIN course_event AS ce ON cm.course_id = ce.course_id
WHERE ce.course_event_id = 223
and help_text IS NOT NULL
AND help_text <> '';
取决于你需要的......
但是这样你就可以获得两次列类型了!