0

I have 4 different tables that are used to gather information from the user, and I'm trying to write a query to simplify the output so that the responses can all be viewed as a single row. The tables are setup like so:

Surveys

+-----+-----------+
| sID | sTitle    |
+-----+-----------+
| 1   | Survey 1  |
| 2   | Survey 2  |
+-----+-----------+

Entries

+-----+-----+
| eID | sID |
+-----+-----+
| 1   | 1   |
| 2   | 1   |
| 3   | 1   |
| 4   | 2   |
| 5   | 2   |
+-----+-----+

Questions

+-----+-----+------+------------+
| qID | sID | type | question   |
+-----+-----+------+------------+
| 1   | 1   | text | question A |
| 2   | 1   | text | question B |
| 3   | 2   | text | remark A   |
| 4   | 2   | text | remark B   |
+-----+-----+------+------------+

Entry Responses

+-----+-----+-----+-------------+
| rID | eID | qID | response    |
+-----+-----+-----+-------------+
| 1   | 1   | 1   | yes         |
| 2   | 1   | 2   | no          |
| 3   | 2   | 1   | maybe       |
| 4   | 2   | 2   | no          |
| 5   | 3   | 1   | foo         |
| 6   | 3   | 2   | bar         |
| 7   | 4   | 3   | answer      |
| 8   | 4   | 4   | unicorns    |
+-----+-----+-----+-------------+
etc...

And I would like to have a query that displays them as such:

+-----+----------+-----+------------+------------+
| sID | sTitle   | eID | question A | question B |
+-----+----------+-----+------------+------------+
| 1   | Survey 1 | 1   | yes        | no         |
| 1   | Survey 1 | 2   | maybe      | no         |
| 1   | Survey 1 | 3   | foo        | bar        |
+-----+----------+-----+------------+------------+

AND / OR

+-----+----------+-----+------------+------------+
| sID | sTitle   | eID | remark A   | remark B   |
+-----+----------+-----+------------+------------+
| 2   | Survey 2 | 4   | answer     | unicorns   |
+-----+----------+-----+------------+------------+

Depending on which survey is queried as that would be the limiting factor. Essentially, I would like to get all the people who responded to a survey and their answers to the questions which can be different depending on the survey. Have the questions that were asked per survey be a column header, and the answer's be in the rows.

I'm currently doing some long joins and query's to get each individual response per question, and manually adding it to a row so I'm hoping there's a better way to do it.

jassok
  • 421
  • 3
  • 12
  • 1
    Could you write some attempts? – Leandro Bardelli Oct 31 '14 at 15:52
  • Thats my biggest problem right now actually, I think you can do it through concat's and procedures but I'm not entirely sure if that fits for this scenario. Both of those are a bit out of my depth. This is what I've found and I'm currently trying to work off of, but I'm not making much progress: http://stackoverflow.com/questions/17964078/mysql-query-to-dynamically-convert-rows-to-columns-on-the-basis-of-two-columns http://dba.stackexchange.com/questions/47902/how-to-transpose-convert-rows-as-columns-in-mysql – jassok Oct 31 '14 at 16:00

0 Answers0