I have several tables from which I am trying to get some data, and I am very close, but cannot close the deal.
I have the following tables:
- EVENT
- USER
- FRIEND
- USER__FRIEND
- EVENT__INVITATION
USER and FRIEND are linked via table USER__FRIEND (which contains USER_ID and field FRIEND_ID)
EVENT__INVITATION associates an event with FRIENDS (it has EVENT_ID and INVITEE_ID)
I am trying to get all EVENTS where:
- I am the creator of EVENT ($ myUserID = EVENT.CREATOR_ID)
- or I'm being invited to an event ($ myUserID = EVENT__INVITATION.INVITEE_ID)
- or one of my OTHERS is the creator of EVENT ($ myUserID = USER__FRIEND.USER_ID AND EVENT.CREATOR_ID IN (my friends list))
- or one of my FRIENDS is invited to an event ($ myUserID = USER__FRIEND.USER_ID AND EVENT__INVITATION.INVITEE_ID IN (list of my friends))
There are other WHERE conditions around other parameters, but I think I can sort them out myself.
Currently, the only way to get this to work is with the UNION, which I think should be compensated, and if I had the best chops, I could use it.
So the question is, can this be done with a single low-cost query that does not use UNION?
Here is what I have done so far, with the exception of the EVENT events that my friends have invited (23 is the user ID in this case):
SELECT e.* FROM event e LEFT JOIN event__invitation ei ON ei.event_id = e.id LEFT JOIN user__friend uf ON uf.friend_id = ei.invitee_id LEFT JOIN friend f ON f.id = uf.friend_id WHERE (ei.invitee_id = 23 OR e.creator_id = 23 OR uf.user_id = 23 OR f.user_id = e.creator_id) AND e.start_time >= 1348000000
and this is a query with UNION:
SELECT e.* FROM event e INNER JOIN event__invitation ei ON ei.event_id = e.id INNER JOIN user__friend uf ON uf.friend_id = ei.invitee_id WHERE (e.creator_id = 23 OR ei.invitee_id = 23 OR uf.user_id = 23) UNION SELECT e1.* FROM event e1 WHERE e1.creator_id IN ( SELECT f1.user_id FROM friend f1 INNER JOIN user__friend uf1 ON uf1.friend_id = f1.id WHERE uf1.user_id = 23 AND f1.user_id IS NOT NULL );
The request contains more requests that make using UNION undesirable. I have a complex trigger calculation, which I do mainly select, and order the results by this value. I think it can ruin the result set.
Thanks for any help!