Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join Two Queries/Select Statement

Tags:

sql

join

select

I don't know how to explain it. But I am trying to join two select statements/queries. I need to include customer and supplier name in the same table.

Table 1 - j:
Job ID, Customer ID
Table 2 - jl:
Job_Line.Job_ID, Supplier_ID
Table 3 - p:
ID, Name

First Select statement - customer name:

Select name
From p
INNER JOIN j ON p.id = j.customer_id

Second Select statement - supplier name:

Select name
From p
INNER JOIN jl ON p.id = jl.supplier_id

Don't know how to join above two selects, so i could have a table like:

id, customer name, supplier name

I am new to SQL and learning online. I understand the basis, but getting stuck at this finding this complex!

like image 491
Honhaar Goonda Avatar asked May 14 '26 20:05

Honhaar Goonda


1 Answers

This should do the trick

SELECT j.id, pc.name, ps.name
FROM j
INNER JOIN p pc ON j.customer_id = pc.id
INNER JOIN jl ON j.id = jl.job_id
INNER JOIN p ps ON jl.supplier_id = ps.id

Note, pc and ps are table aliases.

like image 107
radoh Avatar answered May 18 '26 18:05

radoh