Tutorialsplane

Codeigniter join 3 tables


Codeigniter join 3 tables It is very simple to join multiple tables in codeigniter using query builder class, $this->db->join(); is used to join two or more than two tables in codeigniter. Here in this article we are going to explain how you can join three tables in Codeigniter.


Codeigniter join 3 tables

Here is an example to join three tables – users, profile_image and city.


$this->db->select('*');
$this->db->from('users');
$this->db->join('profile_image', 'profile_image.user_id = users.id');
$this->db->join('city', 'city.user_id = users.id','left');
$this->db->where('users.id', $id); 
$query = $this->db->get();

On the same way you can join two more than two table in codeigniter.
You can check more details about the query builder class in codeigniter here – Database Query Builder Class Or Main Documentation

The above example will generate the below query-

// Select *from users join profile_image on profile_image.user_id = users.id left join city on city.user_id = users.id where users.id = $id