开发者

How do I grab, sort and display top commenters (PHP/MySQL)?

开发者 https://www.devze.com 2023-04-08 18:55 出处:网络
Its been two days and I still can\'t figure this out: how do I retrieve all the users who commented on a given article, sort them in descending order by the number of tim开发者_Python百科es they comme

Its been two days and I still can't figure this out: how do I retrieve all the users who commented on a given article, sort them in descending order by the number of tim开发者_Python百科es they commented and then display their username and number of times they commented (i.e., michael (17), laurie (14), jenny (10), dennis (6), etc.)?


Without seeing the database structure, it's hard to say. But assuming it's something like this:

Article

  • Id
  • Content

Comments

  • Id
  • ArticleId
  • UserId
  • Comment

The query would look something like this:

SELECT UserId, COUNT(*) as CommentCount FROM Comments WHERE ArticleId = 1 GROUP BY UserId ORDER BY CommentCount DESC;

Then you'll just need to do a JOIN on the user table to get the user's name.

0

精彩评论

暂无评论...
验证码 换一张
取 消