Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多对多MySQL表中的计数和多重分组_Mysql_Many To Many_Grouping_Counting - Fatal编程技术网

多对多MySQL表中的计数和多重分组

多对多MySQL表中的计数和多重分组,mysql,many-to-many,grouping,counting,Mysql,Many To Many,Grouping,Counting,我整天都在纳闷,我做不成 我有一个简单的测试表,以新闻系统为例。我们有新闻、标签和分类。 新闻可以有多个标签和一个类别。我需要的是计算每个类别中每个标签下有多少新闻。例如,我们可以在政治类别下有4条带有一般标记的新闻,在科学类别下有2条带有一般标记的新闻 我的桌子如下所示: news: - news_id - category_id - title categories: - category_id - category_name tags:

我整天都在纳闷,我做不成

我有一个简单的测试表,以新闻系统为例。我们有新闻、标签和分类。 新闻可以有多个标签和一个类别。我需要的是计算每个类别中每个标签下有多少新闻。例如,我们可以在政治类别下有4条带有一般标记的新闻,在科学类别下有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;

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

这是我试图设置您的情况:

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

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

这是我试图设置您的情况:

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

旁注:当你写一篇文章时,我只是在stackoverflow编辑工具栏上发现了块引号的东西……我只是花了很多时间手动在所有东西的前面添加了4个空格!哦!好我的查询没有任何错误,它是有效的,但我认为我得到的结果不好。现在我发现我实际上得到了正确的结果,但因为选择所有数据只是为了测试目的,我的眼睛没有看到这一点,我计算了一些不好的东西。嗯,很抱歉,谢谢你让我明白这一点。旁注:我只是在你写文章的时候在stackoverflow编辑工具栏上发现了block quote的东西……我只是花了很长时间在所有东西的前面手动添加了4个空格!哦!好我的查询没有任何错误,它是有效的,但我认为我得到的结果不好。现在我发现我实际上得到了正确的结果,但因为选择所有数据只是为了测试目的,我的眼睛没有看到这一点,我计算了一些不好的东西。很抱歉,谢谢你向我解释清楚。