Here's what I'm trying to do: Table A has a record of user logins Table B has the user's information
I want to computer the total number of logins for each user in table A, then join that to table B so my outcome is something like....
User 1 Name: John Logged In: 15 times User 2 Name: Mary Logged In: 22 times
Any help is greatly appreciated
You need something like that:
SELECT u.*, count(l.id) AS login_count
FROM user s
LEFT JOIN login l ON u.id = l.user_id
GROUP login.id
SELECT
users.*,
COUNT(user_logins.user_id) as login_count
FROM users
LEFT JOIN user_logins ON user_logins.user_id = users.user_id
GROUP BY users.user_id
If you don't need the users with 0 logins remove LEFT
from the LEFT JOIN
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.