在mysql
中,我想计算每个用户拥有的关注者数量,并且该表没有相关列。我希望显示姓名和关注者总数,但每个人都显示为拥有 1 个关注者,这不是预期的结果。
插入脚本
insert into user (email, password, first_name, last_name) values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8af8e9e6ffe4ebe4bacaf8efeceff8efe4e9efa4e9e5e7" rel="noreferrer noopener nofollow">[email protected]</a>', 'E7WC5qpzFPA1', 'Richmound', 'Clunan');
insert into user (email, password, first_name, last_name) values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="01697574736f304175686f782f6262" rel="noreferrer noopener nofollow">[email protected]</a>', 'MjEUC4', 'Herculie', 'Turn');
insert into user (email, password, first_name, last_name) values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2255504d454b475010624b4c56474e0c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>', 'r7BYgEI0', 'Wenonah', 'Rogier');
insert into user (email, password, first_name, last_name) values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="debfbfa8acbfb3b1a8adb5a7ed9ea9b1b1aab6bbb3bbadf0bdb1b3" rel="noreferrer noopener nofollow">[email protected]</a>', 'FMiUgn66amxW', 'Aleta', 'Avramovsky');
insert into user (email, password, first_name, last_name) values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f084949f9c949f9ec4b088999e97de939f9d" rel="noreferrer noopener nofollow">[email protected]</a>', '2UzOXfYyK', 'Teddi', 'Doldon');
insert into user (email, password, first_name, last_name) values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5139303535283c303f6411652239302334357f323e3c" rel="noreferrer noopener nofollow">[email protected]</a>', '7GVGQDIBt8fa', 'Hinda', 'Addyman');
insert into user (email, password, first_name, last_name) values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f6d7870736c7d66295f6c737e6b7a317c7072" rel="noreferrer noopener nofollow">[email protected]</a>', 'KqklbW', 'Randi', 'Golsby');
insert into user (email, password, first_name, last_name) values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c111409100f13124b3c1d111d061312521f13520917" rel="noreferrer noopener nofollow">[email protected]</a>', 'dwUaaMlu', 'Mason', 'Hulson');
insert into user (email, password, first_name, last_name) values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a515e554851535449027a4e145955" rel="noreferrer noopener nofollow">[email protected]</a>', 'qDegvwr8A1n', 'Kareem', 'Dorkins');
insert into user (email, password, first_name, last_name) values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="157f65707b7e70617d2c55617a65666c3b767a78" rel="noreferrer noopener nofollow">[email protected]</a>', 'WucCOGk8', 'Jillane', 'Penketh');
INSERT INTO `following` ( following_id, follower_id)
VALUES (1, 2), (2, 1), (1, 3), (3, 1), (1, 4), (4, 1), (1, 5), (5, 1), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10);
正在关注
- id
- following_id
- followers_id
用户
- id
- first_name
- last_name
- username
我编写的查询
select first_name, count(follower_id)
from `following`
join `user`
on `user`.id = `following`.id
group by first_name
order by count(follower_id) desc;
预期输出:
user|followers
Richmound | 9
Herculie | 1
Wenonah | 1
Aleta | 1
Teddi | 1
Hinda | 0
Randi | 0
Mason | 0
Kareem | 0
Jillane | 0
最佳答案
很简单。尝试下面的查询。我假设当没有关注者时您也需要用户结果。
SELECT u.first_name, count(f.following_id)followers
FROM [user] u
Left Outer Join following f ON (u.id = f.following_id)
Group By u.first_name
ORDER BY count(f.following_id) DESC
如果您不需要零关注者的用户,则用户内部加入。
SELECT u.first_name, count(f.following_id)followers
FROM [user] u
inner Join following f ON (u.id = f.following_id)
Group By u.first_name
ORDER BY count(f.following_id) DESC
关于mysql - SQL 使用 count 连接两个没有公共(public)列的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68324109/