I have two tables a messages and a comments sections. A messages can have many comments, but a comments can only have one message. I am trying to write a sql select statement that would return the message and all comments referring to that message in one row. Is there a way to do that in mysql? How would I go about it. The comments has a message id which is a foreign key that relates to the id of messages. Here is my ERD diagram
2 Answers
You could use the group_concat function:
SELECT message, GROUP_CONCAT(comment SEPARATOR ';') AS all_comments
FROM messages m
JOIN comments c ON m.id = c.message_id
GROUP BY m.id, message
