phpmysqlcodeigniterleft-join

How to join tables to one parent table related by different columns with a MYSQL query in CodeIgniter


How do I write this MySQL query in CodeIgniter?

SELECT *
FROM t1
LEFT JOIN (t2, t3, t4)
ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)

Solution

  • Try this

    $this->db->select(*);
    $this->db->from('t1');
    $this->db->join('t2','t2.a = t1.a','left');
    $this->db->join('t3','t3.b = t1.b','left');
    $this->db->join('t4','t4.c = t1.c','left');
    

    Alternatively (condensed)

    $this->db->select(*)->from('t1')->join('t2','t2.a = t1.a','left')->join('t3','t3.b = t1.b','left')->join('t4','t4.c = t1.c','left');

    Reference: http://ellislab.com/codeigniter/user-guide/database/active_record.html