Reputation: 957
how to perform sql count(*) query on results of another query performing multiple results ?
I've 3 tables :--
user_subscribe table where I've 'user_id' and other columns.
user table where we have 'user_id' & 'country_id' columns where user_id is being linked with user_subscribe table and country_id is being linked with 'country' table.
I need to get total count of user_subscribe based on users (user_id) who belongs to same country. A Help is highly appreciated as I'm being stuck on this problem from last 7 days.
Upvotes: 1
Views: 349
Reputation: 668
Try This Solution :
SELECT C.CountryName,Count(u.UserId) AS UserCount FROM User_Subscribe AS us
LEFT JOIN User AS u ON us.UserId = u.UserId
LEFT JOIN Country AS c ON u.CountryId = c.CountryId
WHERE c.CountryId = 3
GROUP BY c.CountryName
Upvotes: 1
Reputation: 7625
SELECT COUNT(US.user_id) as country_user_subscribed
FROM user_subscribe as US
LEFT JOIN users as U ON user.id=US.user_id
LEFT JOIN countries as C on C.id=U.country_id
GROUP BY C.country_id
Upvotes: 2