2

期待どおりに動作しないクエリに取り組んでいます。正しい方向に向けるのに助けていただければ幸いです。

テーブル 私は3つの中央テーブルを持っています。

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 に答える