在多对多MySQL表中计数和多个分组

时间:2011-12-21 20:24:11

标签: mysql many-to-many grouping counting

我整天都在想,我无法完成它。

我有一个以news系统为例的简单测试表。我们有newstagscategoriesNews可以有多个tags和一个category。我需要的是计算每个news中每个tagcategory的数量news。例如,我们可以在政治类别下使用4 news一般标记,在科学类别下使用一般标记2 news: - news_id - category_id - title categories: - category_id - category_name tags: - tag_id - tag_name news_tags: - news_id - tag_id

我的表格如下:

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;

这是一个简单的MindMap,用于阐明我的需求: enter image description here http://i.stack.imgur.com/6ySiJ.png

以下是我尝试过的查询但没有成功:

{{1}}

1 个答案:

答案 0 :(得分:0)

您的查询对我来说没有错误。 运行查询时会出现什么错误/为什么不成功?

这是我尝试设置你的情况:

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 |
+---------------+----------+------------+