Need help with a query

I have the following table/data structure as shown in the picture:

The result I would like to get is 4 rows like this (each result row is one blue set combined in the picture):
answer | c_id | question_id | user_id

4 | 27838263 | 252 | 28
2 | 27838263 | 252 | 29
1 | 27838263 | 252 | 25
1 | 27838263 | 253 | 25

Any ideas how to proceed with this? Tried some group by’s without success. Or would it be wiser to select the rows as seen in the picture and process it in the code instead of doing it in SQL? I am using MySQL.

Best regards,

I think I solved it. At least seems like its giving the correct result. If anyone has other suggestion feel free to share.

SELECT DISTINCT answer, user_id, question_id, (CONCAT(question_id, user_id, answer)) FROM answer WHERE c_id = 27838263


It is a good option. I don’t know why you’re concatenating those fields but… if it is good for you, it is good for me.

1 Like

I’ll try to explain the reason behind the concatenating. When user gives answer “4” for example for some course (c_id, in this case 27838263) he can choose the events belonging to this c_id and the same answer will be saved for each chosen event for the course (c_id).

So I have two snenarios where I need to pull data from this table. Per event answers, which is easy just using the event number for that.

And the second scenario where I have to distinguish answers given for the whole course (for example the most upper answer set in the picture I do not want 5 x “4” answers, I only want one “4” for one question and one user for the course). For this purpose I concencate the c_id, question_id and user_id fields, which gives me one answer row per user, per question per course (using DISTINCT in the select now to get the desired result). Not sure if this makes more sense :slight_smile:


No. Did you try without the concat? The DISTINCT already does what you want, the concat has nothing to do with it (since it contains the same three fields you already select.


So true :slight_smile: Thanks for the replies.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.