Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple LEFT JOINs in SQL?

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?

like image 378
cute Avatar asked Feb 09 '11 20:02

cute


1 Answers

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.

like image 131
btilly Avatar answered Oct 08 '22 18:10

btilly