mySQL count occurrences with JOIN -


i have tagging system events system create 'tag cloud'.

i have events, can have multiple 'categories'.

here's table structure:

**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           |    

summary:

event id 1 -> sport event id 2 -> charity event id 3 -> sport, charity 

i'd return following:

| tag_name  | occurrences | +-----------+-------------+ |  sport    |     2       | |  society  |     2       | 

other_tag - not returned, has 0 occurrences

thanks! :)

this work:

select c.name tag_name, count(ec.event_id) occurrences  event_categories c  inner join events_categories ec on c.id = ec.event_category_id  group c.id 

change inner join left join if want include categories 0 occurances


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -