mysql - SQL: how to find people that have attended only one event -


given schema , data:

create table contacts (id int, name varchar(255)); create table events (id int, name varchar(255)); create table tickets (contact_id int, event_id int);  insert contacts (id, name) values (1, 'alice'), (2, 'bob'), (3, 'charlie'); insert events (id, name) values (1, 'event 1'), (2, 'event 2'); insert tickets (contact_id, event_id) values (1,1), (1,2); insert tickets (contact_id, event_id) values (2,2); 

how find people have attended 1 event? expect find bob, has attended 1 event, not alice has been 2 events , not charlie has attended zero.

potentially there hundreds of contacts , events, , want find contacts particular event have not attended events before.

i'm drawing blank on one.

edit: let me clarify,

how find people have attended 1 specific event? event 2, expect find bob, has attended 1 event, not alice has been 2 events , not charlie has attended zero.

@yang's answer work users have attended 1 unspecific event, however, if want find ones attending particular first , one, work if tickets.event_id cannot null:

select t1.contact_id tickets t1 left join tickets t2     on t2.contact_id = t1.contact_id     , t2.event_id != t1.event_id t1.event_id = 'someid'     , t2.event_id null; 

a proper index on (tickets.contact_id, tickets.event_id)is assumed performance..


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -