Skip to content
Advertisement

MySQL generate a sublist of comment in a query

I have a database of 3 tables Question, Answer, Comment it works as it is supposed to, each Question can have multiple Answers and each Answer can have multiple Comments

I would like when i query an answer and its comments i get the comments in a List such as:

Is this possible? i have tried to use this query:

And obviously it gives me back this:

I have tried also using the group by in the previous query like this:

And it gives me back this:

Advertisement

Answer

You can use json_arrayagg():

Here is a db<>fiddle.

If you want all rows, even those with no comments, you can use:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement