I have some tables. Most important ones in this JOIN query are:
table 'changes' columns:
|----|------------|----------|------------|-----|----------------|
| id | teacher_id | class_id | subject_id | day | new_teacher_id |
|----|------------|----------|------------|-----|----------------|
|----|--sample input:--------|------------|-----|----------------|
|---1|-----------1|---------1|-----------1|2002-02-02|----------2|
table 'teachers' columns:
|----|---------|
| id | teacher |
|----|---------|
|sample input:-|
|---1|mr. Johnson|
|---2|mr. John-|
|--------------|
The query below works fine:
SELECT c.day, t.teacher, cl.class, s.subjectname
FROM changes AS c
LEFT JOIN teachers AS t ON c.teacher_id = t.id
JOIN classes AS cl ON c.class_id = cl.id
JOIN subjects AS s ON c.subject_id = s.id
But I also want to make something like:
LEFT JOIN teachers AS t ON c.teacher_id = t.id AND c.new_teacher_id = t.id
That does not work.
Any ideas how to JOIN two columns from the same table with one key column from another?
You can join a table multiple times by giving it separate aliases: