We have 2 tables. Question which has a question and Answers which has possible answer's for that question.
I want to collect all the questions such that all the answers for a given question appear in form of array.
select Answers.question_Id as questionId,
group_concat(json_array(Answers.answer)) as answers
from Question
inner join Answers
on Question.id = Answers.question_Id
group by questionId
When I tried the above query I get the following output :
------------------------------------
questionId | answers
--------------------------
1 | ["1"],["2"],["3"],["4"]
------------------------------------
So I tried the following query :
select Answers.question_Id as questionId,
json_array(group_concat(Answers.answer)) as answers
from Question
inner join Answers
on Question.id = Answers.question_Id
group by questionId
I get following output :
questionId | answers
--------------------------
1 | ["1,2,3,4"]
--------------------------
What I am looking for is ['1', '2', '3', '4']
Any suggestions what would be a good way. I also tried using only group_concat but it separates the output by comma and there are high chances that the answer will have comma in it. I was looking for a simple solution.
MySQL version : 5.7.19