Sumit
Sumit

Reputation: 957

sql count(*) query on results of another query performing multiple results

how to perform sql count(*) query on results of another query performing multiple results ?

I've 3 tables :--

  1. user_subscribe table where I've 'user_id' and other columns.

  2. 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.

    1. country table having 'country_id' column.

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

Answers (2)

Kaval Patel
Kaval Patel

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

Chintan7027
Chintan7027

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

Related Questions