Andrew Font Andrew Font - 6 months ago 12
SQL Question

How would I use a SQL query for the following result

I have a database with the following entities:
Survey Model

Question Model

Survey Question

I also have an employee entity not shown here that has an id and name field that I would like to include in my query.

I need to create a query that if given a survey ID would return a list of results that look like this:

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

Answer

I can't test the query but i think you will get the idea:

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>