0

I have to implement a functionality where I have to show answers of question and some comments on that answer, like stackoverflow.

Currently there are 4 comments in each answer but I want to limit the group by to take 2 comments from each answer.LIMIT will work for entire results.

SELECT ans.answer_id,
GROUP_CONCAT(comm.comment_id) AS comment_ids

FROM `questions_answers` AS ans 
JOIN `questions_answers_comments` AS comm
ON comm.answer_id = ans.answer_id

GROUP BY ans.answer_id LIMIT 100
answer_id   comment_ids     
1           1,2,3,4
2           5,6,7,8
3           9,10,11,12
4           16,13,14,15
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • I linked to an answer about limiting groups to four rows per group, but the method is the same whether you want four or two or any other number of rows per group. – Bill Karwin May 17 '21 at 17:14
  • you use https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=53380587f711a8f369b2725f8914e660 – nbk May 17 '21 at 18:01

0 Answers0