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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:59:59+00:00 2026-05-29T05:59:59+00:00

here’s my first! quick rundown: SCENARIO i’m creating an app in which you can

  • 0

here’s my first! quick rundown:

SCENARIO
i’m creating an app in which you can challenge another user to do something (e.g. run 4 miles this week, squat 3 plates, etc.). i’m more of a front end guy so i’ve had some issues with some of the DB trolling and what not.

for the friendship relationship of users i am using two rows to show reciprocation. i request you as a friend and my uid and your uid would be put into the relationships table. once you accept it, the reverse would be added.

when it comes to challenges, though, i’d really rather use only one row per challenge and then just update the TYPE as things progress. type 0 = challenge issued, 1 = challenge accepted, 2 = challenge complete.

i have another table with the info of each challenge (terms, begin date, end date, reward, etc.), so each challenge will be associated with a row in the challenge_info table—one more reason why i would like/need to only have one row per challenge in the challenge table.

PROBLEM
now the problem i’ve had is in trying to get a feed of a user’s most recent challenges. so, ou go to Tom’s profile page and you should see a list of his recent activity: “Tom challenged Amy and won! Tom lost to John. etc..”

I can left join the user table on the challenges table and get the challenges of a particular user, only certain types of challenges, and all that sorta thing, but i can’t find an efficient way to get the username of the person who challenged this user or who was challenged by this user.

TABLES

__USER__

ID  UNAME  
1   Tom  
2   John  
3   Timmy  


__CHALLENGES__  
CID    Challenger_ID    Challenged_ID      Type  
1        1                2                 0  
2        1                3                 1  
3        2                1                 2  

CLIFFS
What is the most efficient way to define this type of relationship and how can I join these tables such that I can find all the challenges Tom was a part of but also get the names and relationships (challenger or challenged) of the people he has interacted with?

Thanks for your time and consideration!

  • 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-29T06:00:00+00:00Added an answer on May 29, 2026 at 6:00 am

    As long as Challenger_ID is indexed you would have no problem using 2 left joins (I assume USER.ID is indexed as a PK):

    select U1.uname, U2.uname from USER U1
    left join CHALLENGES C on C.Challenger_ID = U1.ID
    left join USER U2 on C.Challenged_ID = U2.ID
    

    or just use inner join:

    select U1.uname, U2.uname from USER U1, USER U2, CHALLENGES C
    where C.Challenger_ID = U1.ID AND C.Challenged_ID = U2.ID
    

    the execution plan will vary depending on your indexes but you get the idea.

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

Sidebar

Related Questions

Here's my scenario - I have an SSIS job that depends on another prior
Here is the scenario: I'm writing an app that will watch for any changes
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is the scenario. I'm writing my geo-ruby oracle adapter for Ruby On Rails
Here is what is supposed to happen: The moment the user chooses an option
Here is my code sample, let me know if it can be further improved?
Here's the flow that I am trying to achieve: 1) User uploads an audio
Here is the problem. Every single library I develop logs something, however I don't
Here is the exact copy of my code: jsFiddleCode As you can see I
Here is my code, which takes two version identifiers in the form 1, 5,

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.