I am working with a MySQL backend (version 5.7.19), and a LibreOffice Base frontend(version 7.0.6.2 x64) on 64-bit Windows. I have a table that lists personnel with a primary key id. I also have a workorders table that has an "entered by" field and a "reviewed by" field, both of which need to store the id of the personnel who complete those tasks. If I wanted to have two foreign keys in one table pointing to the same table's primary key, what would my SELECT statement need to look like?
In my case, I have a table 'personnel' with two fields with ID as the primary key, thus:
| ID | Name |
|---|---|
| 1 | John Smith |
| 2 | John Adams |
| 3 | Samuel Adams |
Also, a table 'orders' with three fields with entered_by and reviewed_by as foreign keys to personnel.id
| workorder | entered_by | reviewed_by |
|---|---|---|
| 1 | 2 | 3 |
| 2 | 3 | 1 |
I know how to
SELECT WORKORDER, PERSONNEL.NAME AS ENTERED FROM ORDERS JOIN PERSONNEL ON PERSONNEL.ID = ORDERS.ENTERED_BY ORDER BY WORKORDER.ID;
and how to
SELECT WORKORDER, PERSONNEL.NAME AS REVIEWED FROM ORDERS JOIN PERSONNEL ON PERSONNEL.ID = ORDERS.REVIEWED_BY ORDER BY WORKORDER.ID;
but I'm not sure how to put them into a single query, so that I get:
| workorder | entered | reviewed |
|---|---|---|
| 1 | John Adams | Samuel Adams |
| 2 | Samuel Adams | John Smith |