我正在处理一个不符合预期的查询,如果能帮助我指出正确的方向,我将不胜感激。
表格 我有三个中央表格,包括以下内容。
table_categories
- id (int)
- name (varchar)
table_events
- id (int)
- name (varchar)
- created (date time)
table_taxonomy
- cat_id (id from table_categories)
- event_id (id from table_events)
- type (varchar in this example always 'category')
GOAL 计算每个类别中某个日期之后创建的事件。结果应该是这样的:
COUNT NAME ID
3 Rock 1
2 Punk 3
QUERY 这是我提出的查询。问题是结果不关心创建日期,而是抓取所有事件,无论它们何时创建。
SELECT COUNT(*) AS count,
table_categories.name,
table_categories.id
FROM table_taxonomy
INNER JOIN table_categories
ON table_categories.id = table_taxonomy.cat_id
LEFT JOIN table_events
ON table_events.id = table_taxonomy.events_id
WHERE table_taxonomy.type = 'category'
AND table_taxonomy.cat_id IN(2,3)
AND table_events.created > '2012-10-07 05:30:00'
GROUP BY (table_categories.name)