简体   繁体   English

获取数据库中十大发行商的列表

[英]Get the list of ten top publishers in database

I've got following tables in my MySQL database: 我的MySQL数据库中有以下表格:

USERS 用户

iduser nick password iduser昵称密码

ARTICLES 文章

idarticles iduser text idarticles iduser文本

How can I get by one SQL query the list of eg 10 top publishers of articles ordering by the count of added articles? 如何通过一个SQL查询获取例如10个排名靠前的顶级出版商的列表,按添加的文章数量排序? Is there any way to do that? 有什么办法吗? I'm using PHP to ask database. 我正在使用PHP询问数据库。

Yes, this should be quite easy via JOIN and COUNT() . 是的,通过JOINCOUNT()应该很容易。 Something like the following 类似于以下内容

SELECT `users`.`iduser`, COUNT(`articles`.`idarticles`) AS `total_articles`
FROM `users`
INNER JOIN `articles` ON `users`.`iduser` = `articles`.`iduser`
GROUP BY `users`.`iduser`
ORDER BY `total_articles` DESC
LIMIT 10

A little explaining: 一些解释:

  • COUNT() will get you what it says - a count of all relevant entries in articles COUNT()可以让您明白所说的内容- articles中所有相关条目的计数
  • INNER JOIN will pair all entries that belong together (defined via ON ) INNER JOIN将所有属于在一起的条目配对(通过ON定义)
  • GROUP BY tells SQL that you are interested in various results, each differing by iduser (without this, you'd get all articles counted in the first returned row) GROUP BY告诉SQL您对各种结果都感兴趣,每个结果因iduser而不同(不这样做,您将在返回的第一行中统计所有文章)
  • ORDER BY .. DESC is important to get the result in a descending order (so most published first) ORDER BY .. DESC对于以ORDER BY .. DESC获取结果很重要(因此大多数发布在前)
  • LIMIT 10 does exactly that LIMIT 10确实做到了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM