Codeigniter left join query : $this->db->join(table_name, condition, ‘left’); is used for left join in codeigniter. We can left join as many tables using the same syntax. Here in this article, we are going to explain how you can use left join using Codeigniter Database standard.
Codeigniter left join query | Active Records example
Here in this we have joined two tables users and profile_image and we have performed join to get data collectively.
Example 1
$this->db->select('*'); $this->db->from('users'); $this->db->join('profile_image', 'profile_image.user_id = users.id','left'); $this->db->where('users.id', $id); $query = $this->db->get();
The above query will produce the following Query-
// Select *from users left join profile_image on profile_image.user_id = users.id where users.id = $id
Example 2
Here is another example of left join in Active Records.
$this->db->select('o.id, o.grand_total, o.sub_total, u.name'); $this->db->from('users u'); $this->db->join('sales_order o', 'o.user_id = u.id', 'left'); $this->db->where('o.user_id', 100); $query = $this->db->get();