返回介绍

solution / 1700-1799 / 1729.Find Followers Count / README_EN

发布于 2024-06-17 01:03:15 字数 2078 浏览 0 评论 0 收藏 0

1729. Find Followers Count

中文文档

Description

Table: Followers

+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id   | int  |
| follower_id | int  |
+-------------+------+
(user_id, follower_id) is the primary key (combination of columns with unique values) for this table.
This table contains the IDs of a user and a follower in a social media app where the follower follows the user.

 

Write a solution that will, for each user, return the number of followers.

Return the result table ordered by user_id in ascending order.

The result format is in the following example.

 

Example 1:

Input: 
Followers table:
+---------+-------------+
| user_id | follower_id |
+---------+-------------+
| 0     | 1       |
| 1     | 0       |
| 2     | 0       |
| 2     | 1       |
+---------+-------------+
Output: 
+---------+----------------+
| user_id | followers_count|
+---------+----------------+
| 0     | 1        |
| 1     | 1        |
| 2     | 2        |
+---------+----------------+
Explanation: 
The followers of 0 are {1}
The followers of 1 are {0}
The followers of 2 are {0,1}

Solutions

Solution 1: Grouping and Aggregation

We can directly group the Followers table by user_id, and use the COUNT function to count the number of followers for each user.

# Write your MySQL query statement below
SELECT user_id, COUNT(1) AS followers_count
FROM Followers
GROUP BY 1
ORDER BY 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文