简体   繁体   中英

SQL join two tables without common column using count

In mysql I am wanting to count the number of followers each user has and the table doesn't have related columns. I would like the name and the total followers to display, but each person is displaying as having 1 follower which isn't the expected outcome.

Insert Scripts

insert into user (email, password, first_name, last_name) values ('rclunan0@reference.com', 'E7WC5qpzFPA1', 'Richmound', 'Clunan');
insert into user (email, password, first_name, last_name) values ('hturn1@tiny.cc', 'MjEUC4', 'Herculie', 'Turn');
insert into user (email, password, first_name, last_name) values ('wrogier2@intel.com', 'r7BYgEI0', 'Wenonah', 'Rogier');
insert into user (email, password, first_name, last_name) values ('aavramovsky3@woothemes.com', 'FMiUgn66amxW', 'Aleta', 'Avramovsky');
insert into user (email, password, first_name, last_name) values ('tdoldon4@xing.com', '2UzOXfYyK', 'Teddi', 'Doldon');
insert into user (email, password, first_name, last_name) values ('haddyman5@4shared.com', '7GVGQDIBt8fa', 'Hinda', 'Addyman');
insert into user (email, password, first_name, last_name) values ('rgolsby6@slate.com', 'KqklbW', 'Randi', 'Golsby');
insert into user (email, password, first_name, last_name) values ('mhulson7@amazon.co.uk', 'dwUaaMlu', 'Mason', 'Hulson');
insert into user (email, password, first_name, last_name) values ('kdorkins8@t.co', 'qDegvwr8A1n', 'Kareem', 'Dorkins');
insert into user (email, password, first_name, last_name) values ('jpenketh9@topsy.com', '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);

Following

 - id
 - following_id
 - followers_id

User

- id
 - first_name
 - last_name
 - username

the query that I have written

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; 

Expected output:

user|followers
Richmound | 9
Herculie  | 1
Wenonah   | 1
 Aleta    | 1 
Teddi     | 1
Hinda     | 0
Randi     | 0
Mason     | 0
Kareem    | 0
Jillane   | 0

SQL is utilized to relate tables utilizing keys, finding where they are equal, not equal, or a mixture, and lastly union or union all. If the tables have no characteristic that relates the two, then I would assume you'd want to build a running sum of 1 on both tables, then join that value. I can't offer syntax or a solution due to not understanding the database. Looks like events and not relational database tables, i'd question data architecture and the application writing the data before writing the SQL.

Crazy sql is not the answer, im sorry i suggested it above.

it's simple. try below query. I am assuming that you need user result also when there is no follower.

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

If you don't need users with ZERO followers then user inner join.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM