0

I need to fetch all entries from a table titled users and JOIN a user from the same table based on a userID in the original users entry . I have a simple JOIN query that completes this requirement but I only want to return two columns (userID and fullName) during the JOIN. Currently I'm returning the whole user entry from the JOIN and obviously the column names overlap. For our application's purposes, I need to rename the columns returned from the JOIN. I'm currently using the query below.

SELECT * FROM users u1

JOIN users AS u2
ON(u1.dealer = u2.userID)

This seems like it should be relatively simple but I can't seem to figure it out. I've searched for hours but haven't found a clear solution.

3
  • Like this SELECT u1.*, u2.userID as dealer_id, u2.fullName as dealer_name ? Commented Jun 16, 2018 at 4:14
  • Your select statement seems to reference two tables that don't exist based on my question. u1 and u2 alias aren't defined in your answer and there's no explanation for the join statement. Down vote for not fully reading the question Commented Jun 16, 2018 at 4:28
  • Read it again select statement reference to aliases defined in your query u1 for first user table in from clause and u2 for second user table in join part, Also its a comment not full answer, If it was i had posted it as an answer, Hope that makes sense Commented Jun 16, 2018 at 13:30

2 Answers 2

2
SELECT u1.userID as userID,u1.fullName as fullName, 
u2.userID as dealeruserID,u2.fullName as dealerfullName 
 FROM users u1 JOIN users AS u2 ON(u1.dealer = u2.userID)

I am assuming you need user data and their corresponding Dealers.

Just a thought - for the dealer users - what is their value of "dealer" ?

Sign up to request clarification or add additional context in comments.

1 Comment

This is a lot closer to what I was thinking. Only issue is that I need the entire entry for u1 and userID and fullName for u2. Any way to accomplish that without specifying every column manually? Also, the dealer column is an integer.
0

With the help from Simone I was able to modify her answer to fit my use case.

SELECT u1.*, 
u2.userID as dealeruserID,
u2.fullName as dealerfullName 
FROM users u1 

JOIN users AS u2 ON(u1.dealer = u2.userID)

2 Comments

This looks identical to what I stated at first glance, And by the way you should have accepted her answer instead of posting this
Your answer was incomplete. Her’s was complete. I have already accepted her answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.