This is my table position:
+----+---------+------+--------+---------+
| id | teacher | cook | doctor | dentist |
+----+---------+------+--------+---------+
| 1 | 3 | 4 | 2 | 1 |
+----+---------+------+--------+---------+
And this is my table people:
+----+-----------+--------+-----+
| id | firstname | name | age |
+----+-----------+--------+-----+
| 1 | Fred | Miller | 42 |
| 2 | Emily | Rose | 32 |
| 3 | Ben | Harper | 38 |
| 4 | Samanta | Jones | 35 |
+----+-----------+--------+-----+
My request from the mySQL database
$pdo = $db->query('
SELECT *, position.id AS id, people.id AS people_id
FROM position
LEFT JOIN people
ON position.teacher=people.id;
ON position.cook=people.id;
ON position.doctor=people.id;
ON position.dentist=people.id;
');
while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
echo "The teacher is "$row['firstname']." ".$row['name'];
echo "The cook is "$row['firstname']." ".$row['name'];
echo "The doctor is "$row['firstname']." ".$row['name'];
echo "The dentist is "$row['firstname']." ".$row['name'];
}
My result is:
The teacher is Ben Harper
The cook is Ben Harper
The doctor is Ben Harper
The dentist is Ben Harper
The result I need:
The teacher is Ben Harper
The cook is Samanta Jones
The doctor is Emiliy Rose
The dentist is Fred Miller
id,positionName) 2- add extra column to people tableposition_id.. then you have simpler query