2011-12-21 92 views
2

我想知道整天,我無法完成它。多對多MySQL表中的計數和多個分組

我有一個簡單的測試表,以news系統爲例。我們有news,tagscategoriesNews可以有許多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 

下面是一個簡單的腦圖,以澄清什麼,我需要: enter image description here 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; 

回答

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

側面說明:我纔剛剛discovere了'塊當你寫一個帖子的計算器編輯工具欄上quote'啄... 。我只是花了永久的手動添加4個空間到所有東西的前面! D'哦! – 2011-12-22 03:02:46

+0

呃...我的查詢沒有出現任何錯誤,但它的工作原理,但我認爲我的結果很糟糕。現在我發現我確實得到了正確的結果,但由於選擇了所有數據(僅用於測試目的),我的眼睛沒有看到這一點,我已經計算出一些不好的東西。那麼,對不起,感謝您向我澄清這一點。 – 2011-12-22 10:39:21