I am currently working on a web application for creating / administering a survey with PHP / MySQL. I looked at several versions of the database tables, and I once again discovered that I might need to rethink the repository of a particular type of response.
Right now I have a table that looks like this:
survey_answers
id PK eid sesid intvalue Nullable charvalue Nullable
id = unique value assigned to each row
eid = Poll question that this answer answers
sesid = polling session (time and date information) id
intvalue = Response value if it is a numeric value
charvalue = response value if it is a text representation
This allowed me to continue to use the mathematical functions of MySQL to speed up processing.
However, I found a new task: storing questions that have multiple answers. An example is:
Which of the following do you like to eat? (select all applicable)
- Culinary recipes for girls
- Bacon
- Corn
- Fat whale
Now that I want to save the result, I'm not sure of the best way to handle this. Currently, I have a table for only a few choices, which looks like this:
survey_element_options
id PK eid value
id = unique value associated with each row
eid = question / element that this parameter is related to
value = text value of this option
With this setting, I then save the returned multiple choice answers to "survey_answers" as comma-separated strings of the element_options row identifiers that were selected in the survey. (for example, something like "4,6,7,9"). I am wondering if this is really a better solution, or if it would be more practical to create a new table that will contain each selected answer, and then refer to a specific answer row, which, in turn, refers to the element and, ultimately, to the survey .
EDIT
for everyone who is interested, here is the approach I got (in the PhpMyAdmin relationship view):

And the rudimentary query for collecting counts for a multiple choice question would look like this:
SELECT e.question AS question, eo.value AS value, COUNT(eo.value) AS count FROM survey_elements e, survey_element_options eo, survey_answer_options ao WHERE e.id = 19 AND eo.eid = e.id AND ao.oid = eo.id GROUP BY eo.value