如何将SQL查询用于以下结果

时间:2016-06-06 18:57:03

标签: mysql sql

我有一个包含以下实体的数据库: Survey Model

Question Model

Survey Question

我还有一个未在此处显示的员工实体,其中包含我想要包含在我的查询中的ID和名称字段。

我需要创建一个查询,如果给出调查ID,则会返回如下所示的结果列表:

Survey.name  |  Survey.submitter_id  |  Survey.subject_id | question.text | survey_questions.answer_value |survey_questions.anonymous

1 个答案:

答案 0 :(得分:2)

我无法测试查询,但我认为你会明白这个想法:

SELECT s.name, sq.submitter_id, s.subject_id, q.text, sq.answer_value, sq.anonymous FROM surveys AS s JOIN survey_questions AS sq ON sq.survey_id = s.id JOIN questions AS q ON sq.question_id = q.id WHERE s.id = <your_value>