Suppose I have statistics on the number of visits by users in each country:
The query is as follows:
SELECT countries_users.user_id, countries.name, count(countries_users.id) as count_visit FROM countries_users LEFT JOIN countries on countries.id = countries_users.user_id WHERE countries_users.user_id IN (111, ...) GROUP BY countries_user.user_id, countries.id
The results are as follows:
user_id | countries | count_visit ---------------------------------- 111 | Norway | 5 111 | Japan | 2 ... | ... | ...
Now, normally, I would do this at the code level. However, for some stupid reason I want to add an extra column to the result set which is the total number of visits by the user, regardless of country.
So, the result will become:
user_id | countries | count_visit | overall_visit -------------------------------------------------- - 111 | Norway | 5 | 7 111 | Japan | 2 | 7 ... | ... | ... | ...
may you need a subquery