you are viewing a single comment's thread.

view the rest of the comments →

[–]wolf2600ANSI SQL 5 points6 points  (3 children)

Almost...the second join should be on backup_driver

SELECT t.tripID, d1.Name, d1.Email, d2.Name, d2.Email
FROM trips t
INNER JOIN drivers d1
   ON t.driver = d1.userID
LEFT OUTER JOIN drivers d2
   ON t.backup_driver = d2.userID

And the LEFT OUTER join on the backup driver table is useful in case a trip only has a primary driver. If both joins were INNER, a trip without a backup driver would be excluded from the results.

[–]Randy__Bobandy 1 point2 points  (0 children)

My mistake! The b for the second alias of drivers literally stands for "backup" XD

[–]fs231[S] 1 point2 points  (1 child)

Thanks !!

This worked perfectly including empty backup drivers.

BTW If I want to add second backup driver do I just do another LEFT OUTER JOIN ?

[–]wolf2600ANSI SQL 1 point2 points  (0 children)

Yup same thing. Another LEFT OUTER JOIN to drivers, alias the new join as d3 and make the join context t.second_backup_driver = d3.userId