[英]How to get a summary using only SQL from 3 tables
I have 3 tables: 我有3张桌子:
Table posts
post_id
category_id
title
Table visits
visit_id
post_id
visit_date
Table categories
category_id
category_name
I need to get something like this (assuming that table visits has 10 rows) 我需要得到这样的东西(假设表访问有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
How can I get a similar result using only MySQL? 如何仅使用MySQL获得相似的结果?
I tried with INNERs, no success 我尝试过INNERs,但没有成功
It's a simple query. 这是一个简单的查询。 You need to join all three tables, and finally group the result by category name.
您需要连接所有三个表,最后按类别名称将结果分组。 It should look like:
它应该看起来像:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.