mysql - retrieve rows via join and datetime query where datetime is in past AND NOT in future -
i have 3 table structure follows (simplified):
users ----- id purchases --------- id user_id events ------ id purchase_id starts_on (datetime)
i want select users have event took place before date in past, not have event in future. can think of asking question, "which of users engaged company attending events, not longer?"
this query being generated in rails , have cleaned follows:
select distinct `users`.id `users` left outer join `events` on events.user_id = users.id left outer join `purchases` on `purchases`.id = `events`.purchase_id events.starts_on <= '2013-07-15' , not events.starts_on >= '2013-08-12' limit 0, 30;
although query retrieves users have events prior first date parameter, still includes users have event starts in future, not want. appears "and not" portion of query not working expected.
i suspected problem use of left outer join instead of inner join, changing made no difference.
help appreciated.
i think need have group function.
select distinct `users`.id `users` left outer join `events` on events.user_id = users.id left outer join `purchases` on `purchases`.id = `events`.purchase_id events.starts_on <= '2013-07-15' or events.starts_on >= '2013-08-12' group `users`.id having min(starts_on) <= '2013-07-15' , max(starts_on) < '2013-08-12' limit 0, 30;
Comments
Post a Comment