Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter JOIN (SELECT query

Tags:

codeigniter

It is possible to generate the following query by CI Query Builder class ?

SELECT name 
       FROM table1 t1 
                   JOIN 
                     (SELECT ID FROM table2 ORDER BY id LIMIT 5) t2 
                   ON t2.id=t1.t2_id 
       WHERE t1.id>5
like image 425
zb' Avatar asked Jan 10 '13 05:01

zb'


1 Answers

in CI3, simply use the fourth parameters for escape

$this->db->from('table')
->join('SELECT id from table2 where something=%s) as T2'),'table.id=T2.id', 'LEFT',NULL)
->get()->row();

Don't forget to escape parameters in your subquery to avoid SQL Injection.

like image 190
Zoé R. Avatar answered Sep 19 '22 13:09

Zoé R.