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 6684619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:57:41+00:00 2026-05-26T04:57:41+00:00

I need some help creating a query for my mySQL database. I have recently

  • 0

I need some help creating a query for my mySQL database. I have recently started using JOIN and IN to select rows from my database, so I apologize for sounding like a noob.

I am not looking for an answer (although one would be nice!) but advice on where I should start with my query would be greatly appreciated. Please do not just post a link to the PHP website or to a tutorial.

In my database, users are “linked”, the order is NOT important.

Table Name: UserLinks

link_ID   User_1   User_2
1         10982*   34982
2         82738    16643
3         10982*   99822
4         78256    10982*

The user that I am focusing on here is user 10982

1) The first part of this query that I would like to create find the ID of the users that this user (10982) is linked to. These users are user 34982, 99822, and 78256. I imagine it is something as simple as SELECT * FROM UserLinks WHERE User_1 OR User_2 = 10982, my issue here is, how do I obtain the values – given that I cannot simply choose a row to return (I considered using an if statement… if the value of User_1 is 10982, then choose User_2. This however, seems redundant.

With this list of Users ID’s, I would like to run the second part of the query, to find which Event_ID’s correspond to this list of users (the ID’s of the users are now UserEv_ID):

Table Name: EventUserTags

ETag_ID   UserEv_ID   Event_ID
1         34982*      289
2         82738       231
3         99822*      990
4         78256*      486

2) The second part of this query will use the list generated from the first query (of user ID’s) to generate a list of Event_ID’s. I know that you can use the IN statement and just dump this list in to the query as an array, but that means making the results of the first part of the query into an array. This again, seems redundant. I would like to know how to properly select these Event_ID’s using the User ID’s… I think that the JOIN Query will work, but I need some advice on how to use this.

The values of Event_ID’s obtained here are 289, 990, and 486. In last part of this query I need to use this list of Event_ID’s generated from the last query to match up with the data of another table, my Events table.

Table Name: Events

Event_ID  Event_Order  
182         8728342    
289         3478792*    
990         1876623*     
486         9617789**     

3) Lastly, I need to use the Event_ID’s obtained from the last query to obtain their corresponding Even_Order. Again, I know this can be done with the IN statement (using an array) but this will not be efficient at all.

The purpose of this query is to start with a single User’s ID, and find the Event_Order of every user this user is linked to.

Any help is really appreciated

  • 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-05-26T04:57:42+00:00Added an answer on May 26, 2026 at 4:57 am

    This can all be done fairly simply in one query which I will write and then explain:

    SELECT
       Event_Order, -- I think this is the one you need, but selecting other fields anyway
       Event_ID,
       User_1,
       User_2
    FROM
       Events
       NATURAL JOIN EventUserTags
       JOIN UserLinks ON (
          UserEv_ID = User_1 AND User_2 = 10982
          OR UserEv_ID = User_2 AND User_1 = 10982
       )
    

    NATURAL JOIN is a short cut for JOIN EventUserTags ON (Events.Event_ID = EventUserTags.Event_ID). It works because the keys you are joining on have the same name.

    Then you join on the respective user IDs only if the link has the user you’re looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
I need some help from the shell-script gurus out there. I have a .txt
I need some help with creating an insert statement for an Access 2007 database
I need some help creating a query. Heres a sample table: Segment Name 1
Hi I need some help on getting started with creating my first algorithm; I
I need some help in creating report using iReport for this layout. I am
I have an advanced query/report that I need help creating in Access 2007. The
I need some help creating this extension method. My view inherits from <%@ Page
Ok need some help, I have a system I'm creating that will allow users
Need some help about with Memcache. I have created a class and want to

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.