CodeIgniter Forums
LEFT JOIN to a subquery using CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: Issues (https://forum.codeigniter.com/forum-19.html)
+--- Thread: LEFT JOIN to a subquery using CI (/thread-63604.html)



LEFT JOIN to a subquery using CI - peterlavin - 11-18-2015

Hi all,

I'm trying to do a left join to a sub-query. When I code a simple LEFT JOIN like...


Code:
         //LEFT JOIN mytable ON mytable.myfield=maintable.myfield

        $this->db->join('mytable','mytable.myfield=maintable.myfield','left');

... everything works fine.

I need to do the following (equivalent SQL)...


Code:
LEFT JOIN(select * from othertable where othertable.id = 100) AS oth ON othertable.id=maintable.id


Codeigniter seems to do two things. It strips out the brackets around the subquery, and also produces SQL syntax as follows...


Code:
JOIN AS oth ON othertable.id=maintable.id USING (left)


This is run on a Mysql database and it fails for 'You have an error in your SQL syntax'

I've tried various permutations of this but the result is the same. My sub-query will always be the same so I can use a string for it. Is there a way that I can inject the sub query string into the middle of the query to achieve a LEFT JOIN on a sub query?

Peter