0

I have 2 tables in mySQL database :

customers
============
customer_id (1, 2 )
customer_name (john, mark)


orders
============
order_id = 123
customer_id = 1
customer_from_id = 2

Idea is to do single query on orders table joining customers table whereby

orders.customer_id = customers.customer_id 
orders.customer_from_id = customers.customer_id

to get the "customer_name" by JOIN(ing) two tables.

So how do i do single query on "orders" and expand all (2) "customer_name" fields so result looks like this :

+--------+------------+---------------------+------------------+---------------------+
order_id  customer_id   customer_order_name   customer_from_id   customer_from_name
+--------+------------+---------------------+------------------+---------------------+
   123       1                  john                  2                 mark  
+--------+------------+---------------------+------------------+---------------------+

It means using same table 2x in a query and aliasing output field "customer_name" 2x with "customer_order_name" and "customer_from_name".

It shall be simple but i am stuck. Any help would be much appreciated.

Thank You.

1
  • What have you tried so far? Show what JOIN queries you've tried and we'll let you know where your going wrong Commented Jul 16, 2013 at 10:19

2 Answers 2

1

Join twice and use prefix and give aliases:

select order_id, buyer.customer_id, buyer.customer_name, seller.customer_id as customer_from_id, seller.customer_name as customer_from_name from orders o
join customers seller on o.customer_from_id = seller.customer_id
join customers buyer on o.customer_id = buyer.customer_id;
Sign up to request clarification or add additional context in comments.

Comments

0
select order_id,  c1.customer_id as customer_id,
c1.customer_name as customer_order_name ,
c2.customer_id as customer_from_id,
c2.customer_name as customer_from_name
from orders o
left join customers c1 using (customer_id)
left join customers c2 on o.customer_from_id = c2.customer_id;

fiddle

3 Comments

Thank You all for Your answers. Will try it out and report back. My error was on second LEFT JOIN . I used again just LEFT JOIN customers USING (customer_id); and got 1st column right but in second i got either same values or NULL. ( Amazed with Your fast responses. Thanks again.)
Booth solutions work perfectly. Solved. (Don't believe it was so simple and i missed it, aliasing orders as o and then using LEFT JOIN second time on "o.customer_from_id = c2.customer_id;") One more Q. Do i need to mark this as CLOSED / SOLVED or do the Admins do that ?
You should accept the answer which really helped you to solve your problem. Once you accept the answer, it will be considered as closed. Also you can upvote all the answers which helped you.

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.