-2

我在这里问了一个问题:mySQL count occurrences with JOIN

这个问题,询问如何在加入等后计算标签的出现次数。

我想知道如何做到这一点,但也要检查事件publish_date,因为我只想包含过去 6 个月或一年内的标签。

**events** 
| id  |    publish_date   |
+-------------------------+
+ 1   | 1377612000        |
+ 2   | 1377612000        |
+ 3   | 1377612000        |

**Event_Categories** (Stores Tags / Categories)
| id  | name      |
+-----------------+
+ 1   | sport     |
+ 2   | charity   |
+ 3   | other_tag |


**Events_Categories** (Linking Table)
| event_id  | event_category_id |
+-------------------------------+
+    1      |       1           |   
+    2      |       2           |   
+    3      |       1           |   
+    3      |       2           |   

返回标签计数的 SQL(不考虑发布日期)SELECT c.name AS

tag_name, COUNT(ec.event_id) AS occurrences 
FROM Event_Categories c 
INNER JOIN Events_Categories ec ON c.id = ec.event_category_id 
GROUP BY c.id

谢谢!:)

4

1 回答 1

0
SELECT c.name AS tag_name, COUNT(ec.event_id) AS occurrences 
FROM event_categories c 
INNER JOIN events_categories ec ON c.id = ec.event_category_id 
JOIN events e ON e.id = ec.event_id
WHERE(e.publish_end_date > (unix_timestamp() -7889229) OR e.publish_end_date = 0)
GROUP BY c.id
于 2013-08-07T14:44:35.520 回答