每日一题-29(二级关注者)

题29:

请写一个sql查询语句,对每一个关注者,查询关注他的关注者的数目。
在这里插入图片描述
上表中: followee:被关注者, follower:关注者

解题思路:
(1)用自链接,连接条件就是a.follower=b.followee;
(2)计算b表follower字段的数量(即关注了这些follower的人的数量),注意follow表中会有重复字段,需加个DISTINCT;
(3)按照b表的followee字段分组,
(4)按照a表的follower字段排序。

select a.follower,count(distinct b.follower) as num
from follow a join follow b
on a.follower=b.followee
group by b.followee
order by a.follower;

猜你喜欢

转载自blog.csdn.net/Txixi/article/details/121679308
今日推荐