Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need SQL guru for a complex query

Tags:

sql

join

mysql

I have several tables that I am trying to get some data out of, and I am very close, but cannot quite close the deal.

I have the following tables:

  • EVENT
  • USER
  • FRIEND
  • USER__FRIEND
  • EVENT__INVITATION

USER and FRIEND are linked via the USER__FRIEND table (which contains a USER_ID and a FRIEND_ID field)

EVENT__INVITATION links an EVENT with a FRIEND (it has EVENT_ID and INVITEE_ID)

I am trying to get all EVENTS where:

  • I am the EVENT creator ($myUserID = EVENT.CREATOR_ID)
  • or I am invited to the event ($myUserID = EVENT__INVITATION.INVITEE_ID)
  • or one of my FRIENDs is the creator of the EVENT ($myUserID = USER__FRIEND.USER_ID AND EVENT.CREATOR_ID IN (list of my FRIENDs))
  • or one of my FRIENDs is invited to the EVENT ($myUserID = USER__FRIEND.USER_ID AND EVENT__INVITATION.INVITEE_ID IN (list of my FRIENDs))

There are some other WHERE conditions around other parameters, but I think I can sort those out on my own.

Right now the only way I could get this to work was with a UNION, which I think must be a cop-out, and if I had better chops I could get around using it.

So, the question is, can this be done with a single, inexpensive query that does not use a UNION?

Here is what I have so far, which accomplishes everything except the EVENTs that my FRIENDs are invited to (23 is the passed in userID 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 the query with the 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
);

There is more to the query that makes the use of the UNION undesireable. I have a complex trig calculation that I am doing in the main select, and am ordering the results by that value. I think may mess up the result set.

Thanks for any help!!

like image 472
Raconteur Avatar asked Oct 04 '12 23:10

Raconteur


1 Answers

How about the following:

-- take only distinct events
SELECT DISTINCT e.*
-- start with the event
FROM event e
-- expand to include all invitees and their user_friend info
LEFT JOIN event__invitation ei 
    ON ei.event_id = e.id
LEFT JOIN user__friend invitee
    ON invitee.friend_id = ei.invitee_id
-- now we join again to user_friend to get the friends of the invitees/the creator
LEFT JOIN user__friend invitedFriend
    ON invitedFriend.user_id = invitee.user_id
        OR invitedFriend.user_id = e.creator_id
-- finally we match on whether any of these friends of friends are myself
LEFT JOIN friend myselfAsAFriend
    ON myselfAsAFriend.id = invitedFriend.friendId
        AND myselfAsAFriend.userID = 23
WHERE 
(   
    -- (1) I am the creator of the event
    e.creator_id = 23 
    -- (2) I am invited to the event
    OR invitee.user_id = 23 
    -- (3 and 4) for this join to match a friend of mine must be invited or the creator
    OR myselfAsAFriend.id IS NOT NULL 
)
AND e.start_time >= 1348000000
like image 82
ChaseMedallion Avatar answered Oct 19 '22 11:10

ChaseMedallion