Mark Van
Mark Van

Reputation: 281

JOIN a table for 2 fields

I have 2 tables, which I need to gather information from for 2 fields

Table A
- ID
- WorkerResponsible
- WorkerTaker

Table B
- Id
- Name
- PersonnelNumber

I want to display the PersonnelNumber (MV) instead of the ID, but I am also not sure how to JOIN because of the 2 field, I have tried the following select code, but I am not sure if I have to join 2 times.

How can I gather the right information for both fields?

SELECT
TableA.WorkerResponsible AS WorkerResponsible,
TableA.WorkerTaker AS WorkerTaker,
TableB.PersonnelNumber AS WorkerSalesResponsibleCode,
TableB.PersonnelNumber AS WorkerSalesTakerCode

FROM TableA
JOIN TableB ON TableB.Id = TableA.WorkerSalesResponsible AND TableB.Id = TableA.WorkerSalesTaker

Upvotes: 0

Views: 43

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

You should join the tableB two times using alias (this way)

SELECT
TableA.WorkerResponsible AS WorkerResponsible,
TableA.WorkerTaker AS WorkerTaker,
tb1.PersonnelNumber AS WorkerSalesResponsibleCode,
tb2.PersonnelNumber AS WorkerSalesTakerCode

FROM TableA
INNER JOIN TableB as tb1 ON tb1.Id = TableA.WorkerSalesResponsible 
INNER JOIN TableB as tb2 ON tb2.Id = TableA.WorkerSalesTaker

Upvotes: 3

Related Questions