Search This Blog

Wednesday, June 15, 2011

JOIN ON multiple conditions

If you want to write an sql join on more than one condition you have to keep in mind that the from clause could just have one condition. Example:

SELECT  * FROM person p, accounts a
  LEFT JOIN preferences  pref ON pref.id = p.pref_id AND pref.name = 'glue'

 will fail, but this

SELECT  * FROM person p
  LEFT JOIN preferences  pref ON pref.id = p.pref_id AND pref.name = 'glue'
  LEFT JOIN accounts a   ON a.id = p.account_id AND a.name = 'snoop'

will work.

No comments:

Post a Comment