我有2个表,我需要从2个字段中收集信息
Table A
- ID
- WorkerResponsible
- WorkerTaker
Table B
- Id
- Name
- PersonnelNumber
我想显示PersonnelNumber(MV)而不是ID,但我也不确定如何加入因为2字段,我尝试了以下选择代码,但我不确定是否必须加入2次。
如何为这两个字段收集正确的信息?
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
答案 0 :(得分:3)
你应该使用别名(这样)加入tableB两次
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