Querying Survey fields and responses

AI-generated summary

jordan-violet is experiencing issues with the Discourse Surveys plugin, where radio buttons, checkboxes, and dropdowns are registering as NULL in the database. isaac explains that the values for these fields are stored in a separate table and provides a SQL query to retrieve the survey responses. The query includes options to filter by user ID, and isaac mentions that the :user_id parameter can be dropped if not needed. jordan-violet appreciates the help and notes that they were unaware of the extra table. The provided query is:

SELECT s.name, s.post_id, sf.question, COALESCE(sr.value, sfo.html) AS value, sr.user_id, sr.created_at as responded_at
FROM surveys s
JOIN survey_fields sf ON sf.survey_id = s.id
JOIN survey_responses sr ON sr.survey_field_id = sf.id
LEFT JOIN survey_field_options sfo ON sfo.id = sr.survey_field_option_id
WHERE s.name = :name
AND (:user_id IS NULL OR sr.user_id = :user_id)
ORDER BY sr.user_id

I’m using this Discourse Surveys plugin: GitHub - discourse/discourse-surveys · GitHub

Upon submitting surveys, I see that radio buttons, checkboxes, and dropdowns seem to register as NULL in the database. It also seems to have registered one answer twice:

The value for

are stored in a separate table.

This should get you were you need to be :slight_smile:

-- [params]
-- null int :user_id
-- string :name

SELECT s.name, s.post_id, sf.question, COALESCE(sr.value, sfo.html) AS value, sr.user_id, sr.created_at as responded_at
FROM surveys s
JOIN survey_fields sf ON sf.survey_id = s.id
JOIN survey_responses sr ON sr.survey_field_id = sf.id
LEFT JOIN survey_field_options sfo ON sfo.id = sr.survey_field_option_id
WHERE s.name = :name
AND (:user_id IS NULL OR sr.user_id = :user_id)
ORDER BY sr.user_id

I have added a per user query option (when left empty it queries all user per survey), but feel free to drop

AND (:user_id IS NULL OR sr.user_id = :user_id)

if you are not interested in that.

Ah, I didn’t even catch that extra table! Thank you!