0

So basically I have a comment table, and replies within. With a separate parent_id column for replies. I wanted to query a random number of comments with a limited number of replies for each comment.

What I did is, I parsed the comments first, got all the primary IDs and went for another query like this

SELECT * FROM Comments WHERE parentID IN (ID1, ID2, ..)

Then I programmatically inserted replies to comments. The thing is, I can limit total replies but not limit replies for each comments. Like 5 reply row for each comment id.

About querying the comment table, I think what I'm doing is not so good performance-wise but the few solutions I found in some SO questions was quite overwhelming to me with very little explanation. So I'm trying to understand little by little and this thing came up out of nowhere. Can't find anything helpful.

Anna
  • 319
  • 5
  • 18
  • In order to get a meaningul answer, you would need to [edit your post](https://stackoverflow.com/posts/58700152/edit) to show us sample data and expected results. – GMB Nov 04 '19 at 19:59
  • Also, which version of MySQL are you using? – GMB Nov 04 '19 at 19:59
  • 1
    This is a "greatest n per group" type problem. Googling that expression will give you lots of ideas like [this](https://stackoverflow.com/questions/9969126/mysql-select-top-x-records-for-each-individual-in-table) – Nick Nov 04 '19 at 20:01
  • Thanks @Nick, knowing the right thing to search is quite hard sometimes. I'll look into it. One problem is, I can't comment yet and so can't ask specific questions to those answers. – Anna Nov 04 '19 at 20:06
  • @Anna use that linked answer and/or the flagged duplicate to write your own query, and if you still have problems, post a new question with table structure, some sample data and your query and I'm sure you will get help. – Nick Nov 04 '19 at 20:08
  • @Nick I'll do that. – Anna Nov 04 '19 at 20:10

0 Answers0