-2

I am developing Content / Comment system.. I want: For each content in array to be returned maximum 10 comments.. I tryed LIMIT 20, but this is limiting the total results, how to limit only the repeated values in column comment_id

$contentidArray is array of content ID's

$sthandler = $conn->prepare("SELECT * FROM comments WHERE content_id in (".$contentidArray.") order by total_reactions desc");

$sthandler->execute();
Halid Kyazim
  • 25
  • 1
  • 8

1 Answers1

0

Check this query for self join to fetch max 10 records per content_id,

select c.* FROM comments c
left join comments c1 ON c.content_id = c1.content_id AND c.id <= c1.id
group by c.id
having COUNT(*) <= 10
order by c.content_id, c.id DESC;
Rahul
  • 17,231
  • 7
  • 35
  • 54