How to use joins in codeIgniter?

Today, We want to share with you join query with where condition in codeigniter.In this post we will show you multiple joins in codeigniter, hear for join query in codeigniter for two table we will give you demo and example for implement.In this post, we will learn about PHP Codeigniter 3 Multiple WHERE Conditions Example with an example.

Join Table Example Using Codeigniter

Codeigniter join where

$this->db->select('*');
$this->db->from('members');
$this->db->join('account_image', 'account_image.member_id = members.id');
$this->db->where('members.id', $id); 
$query = $this->db->get();

Will produce the query

// Select *from members join account_image on account_image.member_id = members.id where members.id = $id

use joins in codeigniter4/CI4

$machineInfoTable =  new ModMachineInfo();//creating the instance
            $data['catData'] = $machineInfoTable->distinct()->
            select('
                           mi_id,mi_slug,mi_name,mi_machine_condition,(select leveles.c_name from leveles where leveles.c_id=machine_info.machine_level) as levelName,
                           leveles.c_name,leveles.c_slug
                            ')
                ->where('mi_status',1)
                ->where('machine_level IN (SELECT leveles.c_id from leveles where leveles.c_status = 1 and leveles.c_slug="'.$slug.'")')
                ->orderBy('machine_info.mi_id','desc')
                ->join('leveles','leveles.c_id=machine_info.machine_level')->findAll();

joins/multiple joins with a group by in Codeigniter

$this->db->query("select shop1.ud_id,shop1.ud_date,shop1.up_lat_profit_date,shop1.ud_currency
                  ,sum(shop1.ud_amount*shop3.dp_percentage/100) as profit
               from shop1
               JOIN `shop2` ON `shop2`.`deposit_id` = `shop1`.`ud_id`
               JOIN `shop3` ON `shop3`.`dp_id` = `shop2`.`daily_profit_id`
               where `shop1`.`ud_status` = 1
               AND `shop3`.`dp_status` = 1
               AND `shop1`.`member_plan_id` = $up_id
               AND shop2.pr_status = 1
               group by shop1.ud_id");

use the WHERE clause

$this->db->select('*')
->from('members')
->where('members.m_id',1)
->join('messages','messages.member_id = members.m_id')
->join('city','city.member_id = members.m_id')
->get();

join more than two tables

$this->db->select('*')
->from('members')
->join('messages','messages.member_id = members.m_id')
->join('city','city.member_id = members.m_id')
->get();

right outer join in Codeigniter

$this->db->select('*')
->from('members')
->join('messages','messages.member_id = members.m_id','right')//this is the right join in codeigniter
->get();

left outer join

$this->db->select('*')
->from('members')
->join('messages','messages.member_id = members.m_id','left')//this is the left join in codeigniter
->get();

chaining (in Codeigniter)

$this->db->select('*')
->from('members')
->join('messages','messages.member_id = members.m_id')
->get();

I hope you get an idea about join query with where condition in codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.