Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8621905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:55:30+00:00 2026-06-12T06:55:30+00:00

I have several tables that I am trying to get some data out of,

  • 0

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!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:55:32+00:00Added an answer on June 12, 2026 at 6:55 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several tables that I am selecting data from. I am trying to
I have a data-upload function that load some data into several tables and processes
I have several database tables that just contain a single column and very few
I'm trying to write a MySQL script that creates several tables. I have: CREATE
I have several pages that access my data-base directly and get values from a
I have several tables that I want to put side by side, however when
I have a function that updates several tables. As an example, let's say it
I have an Excel Workbook that has several worksheets in it. The tables in
I have a table with data relating to several moments in time that I
I have spent the last several hours trying to get this to work and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.