父表按子表数量排序
我有一个 tbl_user ,其中包含有关用户的信息,还有一个 tbl_article ,其中包含文章 + tbl_user 的 ID
我们有父子关系,因为每个用户可能有很多文章,这就是为什么我在文章中包含 user_id桌子。
我想列出拥有最多文章的 10 个用户...我到处搜索,但找不到它...我想过,但徒劳,我不擅长 SQL 查询。
先感谢您
I have a tbl_user , which contains information about user, and I have a tbl_article, which contains articles + the ID of the tbl_user
We have a parent-child relation, because every user may have many articles, that's why I included user_id in the articles table.
I'd like to list the 10 users that have most articles... I've searched everywhere though I couldn't find it...I've thought about it , but in vain, I'm not good in SQL Queries.
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您使用的 RDBMS,您可能需要
TOP(10)
或LIMIT 10
等。我将两者都包含在内,以便您可以看到,但仅使用所使用的那个由您的 RDBMS ;)Depending on which RDBMS you use, you may need
TOP(10)
orLIMIT 10
, etc. I included both so you can see, but only use the one that is used by your RDBMS ;)您所需要的只是一个
GROUP BY
和一个JOIN
。如果您希望包含有 0 篇文章的用户,您应该使用
LEFT JOIN
。如果担心重复,您还可以选择
COUNT(DISTINCT Article)
。All you need is a
GROUP BY
and aJOIN
.If there is a potential for users with 0 articles that you want to include, you should use a
LEFT JOIN
.Optionally you can also
COUNT(DISTINCT Article)
if there is a concern about duplicates.