【问题标题】:mySQL > Limit the same amount of value returned [duplicate]mySQL > 限制返回的相同数量的值 [重复]
【发布时间】:2019-10-21 01:42:54
【问题描述】:

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

$contentidArray 是内容 ID 的数组

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

$sthandler->execute();

【问题讨论】:

  • 所以你想添加一个LIMIT 10
  • 你面临什么问题?

标签: php mysql pdo


【解决方案1】:

检查此查询以获取自联接以获取每个 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;

【讨论】:

  • 谢谢,我现在就试试..
猜你喜欢
  • 2013-04-17
  • 2010-12-11
  • 1970-01-01
  • 2017-07-03
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多