我有3张桌子:
Table posts
post_id
category_id
title
Table visits
visit_id
post_id
visit_date
Table categories
category_id
category_name
我需要获取类似的信息(假设表访问有10行)
Category 1 (categories.category_name) / visits(count) = 5
Category 2 (categories.category_name) / visits(count) = 1
Category 3 (categories.category_name) / visits(count) = 4
仅使用MySQL如何获得相似的结果?
我尝试过INNERs,但没有成功
答案 0 :(得分:1)
这是一个简单的查询。您需要连接所有三个表,最后按类别名称将结果分组。看起来应该像这样:
select
c.category_name,
count(*)
from categories c
join posts p on p.categoryid = c.category_id
join visits v on v.post_id = p.post_id
group by c.category_name
order by c.category_name