我在这里问了一个问题: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
谢谢!:)