与朋友最多的前5名 [英] Top 5 with most friends

查看:27
本文介绍了与朋友最多的前5名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SQL 的新手,我正在尝试弄清楚如何与大多数朋友 (userId) 获得前 5 名乐队",这就是我所拥有的;一个 usertbl 以 userId 作为 PK 然后一个 bandTbl 以 bandId 作为 PK 然后我有一个表 bandfriends 与 FK userId 和 bandId.

Hi I'm new to SQL and I'm trying to figure out how I'm going to get the top 5 "bands" with most friends (userId) and this is what i have; a usertbl with userId as PK then a bandsTbl with bandId as PK then I have a table bandfriends with FK userId and bandId.

bandfriends
userid | bandId
---------------
 1     | 1
 1     | 2
 1     | 3

谢谢!

推荐答案

在 mysql.org 上阅读 COUNT 和 GROUP BY

Read up on COUNT and GROUP BY at mysql.org

你会想要这样的东西(我还没有测试过):

You'll want something like this (I haven't tested it):

SELECT bandId, COUNT(*) as fans FROM bandfriends
ORDER BY fans DESC
GROUP BY bandId
LIMIT 5;

这篇关于与朋友最多的前5名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆