2

私は一日中疑問に思っています、そして私はそれを成し遂げることができません。

news例としてシステムを使用した簡単なテストテーブルがあります。news、、、がtagsありcategoriesます。 News多くtagsと1つを持つことができますcategory。私が必要なのは、それぞれnewsの下にいくつあるかを数えることです。たとえば、政治カテゴリの下に一般タグが付いた4つと、科学カテゴリの下に一般タグが付いた2つを設定できます。tagcategorynewsnews

私のテーブルは次のようになります。

news:
    - news_id
    - category_id
    - title

categories:
    - category_id
    - category_name

tags:
    - tag_id
    - tag_name

news_tags:
    - news_id
    - tag_id

これが私が必要なものを明確にするための簡単なマインドマップです:http: enter image description here //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 に答える