F.Kncja F.Kncja - 8 months ago 53
SQL Question

Query to match people with ID

Let's suppose we have 2 tables.
First is for some ORDER: salesperson_id, customer_id, date, price... etc
Second is for people record PERSON: person_id, name, mail... etc

I need to select the order with both customer and salesperson names.
So row of records looks like this

order.order_id, order.date, name (for customer), name (for salesperson)

I don't want to display ids for people, only names.

I cannot join with the PERSON table twice, so, my question is how do I do that?


Yes you can - join the person table twice but with different alias names.

select o.order_id, o.date, 
       c.name as customer, 
       s.name as sales_person
from order_table o
left join person_table c on c.id = o.customer_id
left join person_table s on s.id = o.salesperson_id