CodeIgniter Forums

Full Version: LEFT JOIN to a subquery using CI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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