1

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.

1
  • **sql count() query on results of another query returning multiple results Commented May 8, 2018 at 5:34

2 Answers 2

2
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
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

add where condition for that i am updating my query
Hi Kavel thanks alot for responding, Your Query is working properly without any problem. However, the issue is still stuck as I need to find result based on the country_id which I already have. For Example , India has country_id = 3 So I need to find out total subscriber from 'user_subscribe' table based on 'user_id' which belongs to country_id=3 (India).
please check query now and if it work then please accept and vote answer so it also helps other
Thanks Kavel it is working perfect for us, However I don't really think that 'group by' is necessary as per our requirement. Cheers to you anyways. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.