很难根据用户的国家/地区视图提取文章
我有以下表格及其字段
user: id, country_id
article: id
article_views: id, user_id, article_id
每次用户查看文章时,我都会将其插入到article_views表中,如下所示:
article_views.id article_id user_id
2 1 1
3 2 1
4 2 2
5 2 2
我想为当前的user.country_id提取浏览量最高的文章。我想它会包含:
order by article_views.article_id DESC
有什么建议吗?
答案 0 :(得分:4)
这应该可以正常工作:
select article_id, count(*) as views
from article_views inner join user on article_views.user_id = user.id
group by user.country_id
order by views desc;
编辑。我忘了将article_id分组为@Josien指出,正确的查询是:
select country_id, article_id, count(*) as views
from article_views inner join user on article_views.user_id = user.id
where country_id = ':countryID'
group by user.country_id, article_id
order by views desc;
答案 1 :(得分:2)
select av.id, count(*) as views
from article_views av inner join user u on av.user_id = u.id
where u.country_id = 'cc'
group by u.country_id
order by views desc;