mysql - SQL query returning events with an odd number of users assigned to them -


i have 5 tables--users, groups, events, users_groups_link, users_events_link. so:

users user_id  groups group_id  events event_id  users_groups_link user_id group_id  users_events_link user_id event_id 

each event has 2 or 4 groups assigned , each group contains 8-11 users. user_id=1 group vacancy. i'm wanting make sure each event has number of users i'm trying create query returns events containing odd number of users. far have this:

select user_id,event_id  users,groups,events,users_events_link,users_groups_link users.user_id=users_events_link.user_id , events.event_id=users_events_link.event_id , users.user_id=users_groups_link.user_id , groups.group_id=users_groups_link.group_id , user_id <> 1 

which returns events , users assigned them (via host group) so:

user_id         event_id 56              1001 34              1001 76              1001 45              1001 87              1001 88              1001 54              1001 4               1001 63              1002 69              1002 77              1002 etc. 

now i'm wanting refine query further returning events odd number of users assigned them i.e. events id in right hand column repeating odd number of times. , maybe refining further eliminating users_id column entirely , returning 1 event id each event containing odd number of users.

i've tried using "count()" , "group by" not getting desired results i'm doing wrong don't know what. in advance.

you can use combination of count, having , modulo operator:

select event_id, count(distinct user_id) c users,groups,events,users_events_link,users_groups_link users.user_id=users_events_link.user_id , events.event_id=users_events_link.event_id , users.user_id=users_groups_link.user_id , groups.group_id=users_groups_link.group_id , user_id <> 1 group event_id having c % 2 = 1  

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -