MySQL>限制返回的相同数量的值

时间:2019-06-05 14:13:46

标签: php mysql pdo

我正在开发内容/评论系统。 我想要:对于数组中的每个内容,最多返回10条评论。 我尝试过LIMIT 20,但这限制了总结果,如何仅限制列comment_id中的重复值

$contentidArray是内容ID的数组

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

$sthandler->execute();

1 个答案:

答案 0 :(得分:0)

检查此查询是否进行自我连接,以每个content_id最多提取10条记录,

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;