Jump to content

Recommended Posts

So here's my situation: I have a table called User and Report. The Report table has 2 columns, 1 for the person submitting the report and another for the user they are reporting. Each column contains the user id of the respective user. The User table has user_id and user_username. I know how to do a simple Join to get one of the usernames, but how do I get both?

 

For example: to get the username of the person submitting the report, my query is:

SELECT r_id,user_username,r_reason FROM Report,User WHERE User.user_id=Report.r_reporter

 

This gives me results with theusername who submitted the report, but I also need to grab the username of the person being reported

 

 

 

EG

 

SELECT r.R_id, a.user_username as userA, b.user_username as userB
FROM report r
INNER JOIN user a ON r.userid1 = a.userid
INNER JOIN user b ON r.userid2 = b.userid

 

I don't know your column names so can't be precise. Join one id col to user with one alias and the second id to user with another alias

But I have shown him a far better way to define joins rather than

 

... FROM tableA, tableB WHERE tableA.something = tableB.something

 

thus separating the structure of  the query from the selection conditions

EG

 

SELECT r.R_id, a.user_username as userA, b.user_username as userB
FROM report r
INNER JOIN user a ON r.userid1 = a.userid
INNER JOIN user b ON r.userid2 = b.userid

 

I don't know your column names so can't be precise. Join one id col to user with one alias and the second id to user with another alias

 

yes! this worked perfectly, thank you!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.