2

I'm working on a query that uses COALESCE for retrieving localized strings for my current project

The sql works when used as a raw query but i cant seem to get the joins structured in this way with the query builder

LEFT JOIN text as text_se
ON (main_table.text = text_se.id)
AND (text_se.lang_code = 'SV')

This code:

->join(array('text', 'text_se', 'LEFT')
    ->on('main_table.text', '=', 'text_se.id')
    ->on('text_se.lang_code', '=', 'SV')

Only results in

LEFT JOIN text as text_se
ON (main_table.text = text_se.id AND text_se.lang_code = 'SV')

Which doesn't do the same... Have anyone else used AND after the JOIN-statement and can help me with it?

Johan Tell
  • 21
  • 1
  • 2

3 Answers3

5

Or you could do this....

->join(array('text', 'text_se'), 'LEFT')
    ->on('main_table.text', '=', DB::Expr('text_se.id AND text_se.lang_code = "SV"'))
pogeybait
  • 3,065
  • 2
  • 21
  • 23
1

Why not use where:

->join(array('text', 'text_se', 'LEFT')
->on('main_table.text', '=', 'text_se.id')
->where('text_se.lang_code', '=', 'SV')
matino
  • 17,199
  • 8
  • 49
  • 58
  • This may work in this instance, but the where parameter will affect only the where clause. In some instances it's desirable to join tables on multiple conditions to further optimize queries. – Mike Purcell Dec 30 '14 at 00:02
  • in this case LEFT JOIN will act like INNER JOIN – Kalim Jul 27 '15 at 10:05
1

This appears to work in 3.1.x:

->join(array('text', 'text_se', 'LEFT')
->on('main_table.text', '=', DB::expr('text_se.id'))
->on('text_se.lang_code', '=', DB::expr('SV'))
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89