2

我整天都在想,我不能完成它。

我有一个以news系统为例的简单测试表。我们有newstagscategoriesNews可以有很多tags和一个categorynews我需要的是计算每个tag中每个下有多少个category。例如,我们可以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

这是一个简单的思维导图来阐明我需要什么: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;
4

1 回答 1

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 |
+---------------+----------+------------+
于 2011-12-22T03:01:51.523 回答