I want to fetch column EmployeeName from Table A associated with toEmployeeId and fromEmployeeId in Table B using one query only.
Here are my tables
Table A
|---------------------|------------------|
| Employeeid | EmployeeName |
|---------------------|------------------|
| E1 | ABC1 |
|---------------------|------------------|
| E2 | ABC2 |
|---------------------|------------------|
| E3 | ABC3 |
|---------------------|------------------|
Table B
|---------------------|------------------|
| toEmployeeid | fromEmployeeid |
|---------------------|------------------|
| E1 | E3 |
|---------------------|------------------|
| E2 | E1 |
|---------------------|------------------|
| E3 | E1 |
|---------------------|------------------|
Here is the query I am trying to run but it is not working
select A.Employeeid, A.employeename,
(select employeename
from A,
B
where A.employeeid = B.fromemployeeid)
from A,
B
where B.toEmployeeid = A.id;
You should use the same table A two time with proper alias A1 e A2
select B.toEmployeeid
, A1.EmployeeName
, B.fromEmployeeid
, A2.EmployeeName
from B
INNER JOIN A A1 ON A1.Employeeid = B.toEmployeeid
INNER JOIN A A2 ON A2.Employeeid = B.fromEmployeeid
This is how I got it to work. For anyone looking for an answer.
select At.Employeeid, At.employeename,
(select employeename from A as At
where At.employeeid = Bt.fromemployeeid) as fromEmpId
from A as At,
B as Bt
where Bt.toEmployeeid = At.Employeeid;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.