10 comments, x most recent replies to each query

Discussion in 'MySQL' started by Jargonaut, Nov 8, 2011.

  1. #1
    I would like to be able to pull the 1st x number of replies to multiple comments in a reduced number of queries. My end result is to have 10 comments displayed on a page with the 2 most recent replies to each.

    Table structure: Comments
    CommentId
    ParentId
    Comment
    ReplyCount

    Let's say there are 10 comments with NULL as the parent Id meaning they are not replies to anything else. If there were say 50 replies to each of those 10 comments, is there a way I could pull just the 1st 2 most recent for each one in a singe query? I've looked into the often recommended tree traversal methods but I can't find a way with them to easily pull 10 comments and a limited number of the replies to each. I'm currently having to contemplate doing one query for the 1st 10 comments and then a single query for each of those to pull out a few replies to each which is not great. Is there a better way to do this?

    Any assistance greatly apreciated.
     
    Jargonaut, Nov 8, 2011 IP