2

我正在处理一个不符合预期的查询,如果能帮助我指出正确的方向,我将不胜感激。

表格 我有三个中央表格,包括以下内容。

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)
4

2 回答 2

3

试试这个,在比较日期时只做一点小改动:

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 Date(table_events.created) > '2012-10-07 05:30:00'
            GROUP BY (table_categories.name)
于 2012-10-08T06:42:32.507 回答
1

试试这个,

SELECT  c.ID, c.Name, COUNT(*)
FROM    table_categories a
        INNER JOIN table_taxonomy b
            ON a.id = b.cat_id
        INNER JOIN table_events c
            ON b.event_ID = c.ID
WHERE   c.created > 'dateHere' 
        -- AND .....                  -- other conditions here
GROUP BY    c.ID, c.Name
于 2012-10-08T06:40:45.233 回答