多对多MySQL表中的计数和多个分组
问题描述:
我想知道整天,我无法完成它。多对多MySQL表中的计数和多个分组
我有一个简单的测试表,以news
系统为例。我们有news
,tags
和categories
。 News
可以有许多tags
和一个category
。我需要的是统计每个category
中每个tag
下有多少个news
。例如,我们可以在政治类别下带有通用标签的4 news
,在科学类别下带有通用标签的2 news
。
我的表是这样的:
news:
- news_id
- category_id
- title
categories:
- category_id
- category_name
tags:
- tag_id
- tag_name
news_tags:
- news_id
- tag_id
下面是一个简单的脑图,以澄清什么,我需要: http://i.stack.imgur.com/6ySiJ.png
下面是一个查询,我没有成功尝试:
SELECT *, COUNT(n.news_id) AS news_count FROM news AS n
LEFT JOIN categories AS c ON n.category_id = c.category_id
LEFT JOIN news_tags AS tn ON n.news_id = tn.news_id
LEFT JOIN tags AS t ON tn.tag_id = t.tag_id
GROUP BY t.tag_id, c.category_id;
答
您的查询无误我的行为。 运行查询时会出现什么错误/为什么它不成功?
这是我尝试设置您的情况:
mysql> select * from news;
+---------+-------------+------------------------+
| news_id | category_ID | title |
+---------+-------------+------------------------+
| 1 | 1 | politics and general 1 |
| 2 | 1 | politics and general 2 |
| 3 | 1 | politics and general 3 |
| 4 | 1 | politics and general 4 |
| 5 | 2 | science and general 1 |
| 6 | 2 | science and general 2 |
| 7 | 2 | science and funny 1 |
+---------+-------------+------------------------+
mysql> select * from tags;
+--------+----------+
| tag_id | tag_name |
+--------+----------+
| 1 | general |
| 2 | funny |
+--------+----------+
mysql> select * from news_tags;
+---------+--------+
| news_id | tag_id |
+---------+--------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
| 7 | 2 |
+---------+--------+
mysql> select * from categories;
+-------------+---------------+
| category_id | category_name |
+-------------+---------------+
| 1 | politics |
| 2 | science |
+-------------+---------------+
查询的结果:
+---------+-------------+------------------------+-------------+---------------+---------+--------+--------+----------+------------+
| news_id | category_ID | title | category_id | category_name | news_id | tag_id | tag_id | tag_name | news_count |
+---------+-------------+------------------------+-------------+---------------+---------+--------+--------+----------+------------+
| 1 | 1 | politics and general 1 | 1 | politics | 1 | 1 | 1 | general | 4 |
| 5 | 2 | science and general 1 | 2 | science | 5 | 1 | 1 | general | 2 |
| 7 | 2 | science and funny 1 | 2 | science | 7 | 2 | 2 | funny | 1 |
+---------+-------------+------------------------+-------------+---------------+---------+--------+--------+----------+------------+
然而,它没有任何意义SELECT *
因为你的标签总计数/类别,像title
这样的东西在聚合时没有意义。
您可以试试:
SELECT c.category_name, t.tag_name, COUNT(n.news_id) AS news_count FROM news AS n
LEFT JOIN categories AS c ON n.category_id = c.category_id
LEFT JOIN news_tags AS tn ON n.news_id = tn.news_id
LEFT JOIN tags AS t ON tn.tag_id = t.tag_id
GROUP BY t.tag_id, c.category_id;
要获取:
+---------------+----------+------------+
| category_name | tag_name | news_count |
+---------------+----------+------------+
| politics | general | 4 |
| science | general | 2 |
| science | funny | 1 |
+---------------+----------+------------+
侧面说明:我才刚刚discovere了'块当你写一个帖子的计算器编辑工具栏上quote'啄... 。我只是花了永久的手动添加4个空间到所有东西的前面! D'哦! – 2011-12-22 03:02:46
呃...我的查询没有出现任何错误,但它的工作原理,但我认为我的结果很糟糕。现在我发现我确实得到了正确的结果,但由于选择了所有数据(仅用于测试目的),我的眼睛没有看到这一点,我已经计算出一些不好的东西。那么,对不起,感谢您向我澄清这一点。 – 2011-12-22 10:39:21