Codeigniter: Select from multiple tables Codeigniter: Select from multiple tables codeigniter codeigniter

Codeigniter: Select from multiple tables


The example in the User Guide should explain this:

$this->db->select('*'); // <-- There is never any reason to write this line!$this->db->from('blogs');$this->db->join('comments', 'comments.id = blogs.id');$query = $this->db->get();// Produces:// SELECT * FROM blogs// JOIN comments ON comments.id = blogs.id

See the whole thing under Active Record page in the User Guide.


Just add the other table to the "->from()" method. Something like:

 $this->db->select('t1.field, t2.field2')          ->from('table1 AS t1, table2 AS t2')          ->where('t1.id = t2.table1_id')          ->where('t1.user_id', $user_id);


I think the question was not so much about joins as how to display values from two different tables - the User Guide doesn't seem to explain this.

Here's my take:

    $this->db->select('u.*, c.company, r.description');    $this->db->from('users u, company c, roles r');    $this->db->where('c.id = u.id_company');    $this->db->where('r.permissions = u.permissions');    $query = $this->db->get();