I have 2 tables in a mysql database ("comments" and "replies")
They have the schemas:
comments: id,userId,text
replies: id,userId,commentId,text
I need a mysql query that will fetch all comments from the comment table, and after each comment, the corresponding replies from the replies table...
so if we had: [comment 1] and [comment 2] in the comments table, and [reply 1] (to comment 1) and [reply 2] (to comment2)
in the replies table
then it would return:
[comment 1]
[reply 1]
[comment 2]
[reply 2]
You would need to join the two tables & then order based on the commentID & replyID for multiple replies.
In the following I have added a dummy replyID of 0 for the original comment. The tables are joined using UNION ALL. Note that I have renamed the original id column of the comments table & reply id so they do not clash.
SELECT id commentID, userID, text, 0 replyID FROM test.comments
UNION ALL
SELECT commentID, userID, text, id replyID FROM test.replies
ORDER BY commentID, replyID;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.