假设我有每个国家用户访问次数的统计:
查询如下:
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
结果如下:
user_id | countries | count_visit ------------------------------- 111 | Norway | 5 111 | Japan | 2 ... | ... | ...
现在,通常情况下,我会在代码层面完成这个操作。然而,由于某种愚蠢的原因,我想在结果集中添加一个额外的列,该列是用户的总访问次数,不考虑国家。
所以,结果将变为:
user_id | countries | count_visit | overall_visit --------------------------------------------------- 111 | Norway | 5 | 7 111 | Japan | 2 | 7 ... | ... | ... | ...
您可能需要子查询
雷雷