How to use multiple LEFT JOINs in SQL?

cute picture cute · Feb 9, 2011 · Viewed 160.4k times · Source

Is it possible to use multiple left joins in sql query?

    LEFT JOIN
        ab 
    ON
        ab.sht = cd.sht

i want to add to attach one more query like this to it? will it work?

    LEFT JOIN
        ab AND aa
    ON
        ab.sht = cd.sht
           AND
        aa.sht = cc.sht

Will this work?

Answer

btilly picture btilly · Feb 9, 2011

Yes it is possible. You need one ON for each join table.

LEFT JOIN ab
  ON ab.sht = cd.sht
LEFT JOIN aa
  ON aa.sht = cd.sht

Incidentally my personal formatting preference for complex SQL is described in http://bentilly.blogspot.com/2011/02/sql-formatting-style.html. If you're going to be writing a lot of this, it likely will help.