How to use Join Query in Codeigniter

So, I am going to show you how to use a join query in Codeigniter. Also, retrieve the data as you want with example.

Here, we have defined many ways to use join query with where condition.

As well as, fetch data multiple methods like left join, right join, full join, and inner join in detail.

Therefore, I am using the example of multiple tables with the multiple join query in Codeigniter.

public function getdata(){	

	$this->db->from('table1'); // this is first table name
	$this->db->join('table2', ' ='); // this is second table name with both table ids
	$query = $this->db->get();
	return $query->result();


Join Query in Codeigniter

Hence, we display some method and full code with the using join query using the table.

So, the retrieved data code with joins table using Codeigniter step by step in the below section.

Step 1: Model ( join query in Codeigniter with example )

class UserModel extends CI_Model{
	public function getdata(){	

	$this->db->join('vendor', 'vendor.userid =');
	$query = $this->db->get();
	return $query->result();



This is a model file and created a class for displaying joins query working in this model class.

Thus, there have used 2 tables to fetch the data from the database. Therefore, staring apply select query and created two tables in the MySQL database.

Similarly, you can apply to join two or three tables using Codeigniter according to this code.

Now we showing the next step for the controller with the function example in Codeigniter. After that explain left, right, and full joins.

Step 2: controller ( Including class and function )

defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	public function index()
		$data['users'] = $this->UserModel->getdata();		
		$this->load->view('user', $data);

Here, I am fetching the model from the model page and store a whole data a variable $data.

Still, created a key which name is user and pass on view section with all data in array form.

Firstly, you loaded a database from this function we define above section $this->load->database().

Then again, the model load is the right way. So, the last line for view part load file and pass a variable of the whole array as we define apply to join query $this->load->view(‘user’, $data).

Step 3: View ( Here are showing table and insert data in Codeigniter )

<!DOCTYPE html>
	<title>User Data with Project</title>
	<h1>This is Join conditions Output</h1>
<table border="2px solid #000 ">
	<?php 	foreach ($users as $userdata) { 
		// echo "<pre>";
		// print_r($userdata);

	<td><?php echo $userdata->name; ?></td>
	<td><?php echo $userdata->address; ?></td>	

<?php } ?>


After all, created a Html structure in view file and getting a variable.

Then, take the Foreach loop including an array variable. After that, the print view in a table forms multiple data.

Output: View page data in table form retrieve from the database using MVC Codeigniter.

join query in codeigniter

Left Join in Codeigniter

Similarly, apply code join but including a left join query. At the same time, given an example that how to use left join in the model file using Codeigniter.

public function getdata(){	

	$this->db->from('table1'); // this is first table name
	$this->db->join('table2', ' =','left'); // this is second table name with both table ids
	$query = $this->db->get();
	return $query->result();


Here, you do that’s a simple example. So, we have to use 2 tables can do more than multiple tables and multiple joins. likewise, two, three, and 4 tables as so on.

Right Join in Codeigniter

As much as, add right join condition in the table. in the below section given an example add right join query in Codeigniter.

public function getdata(){	

	$this->db->from('table2'); // this is first table name
	$this->db->join('table3', ' =','right'); // this is second table name with both table ids
	$query = $this->db->get();
	return $query->result();


Note: If you want to do a simple example whole Codeigniter setup and step by step install file structure. Also, learn the basic project in this Codeigniter tutorial

Where Conditions

Here, you can apply ‘where condition‘ with the join query. Also, given model page code to created where conditions getting data use id from the database.

public function FetchData(){	

	$this->db->from('user'); // this is first table name
	$this->db->join('vendor', ' ='); // this is second table name with both table ids
	$query = $this->db->get();
	return $query->result();


Multiple Joins in Codeigniter

In case, this is a simple way to apply multiple joins using Codeigniter.

Therefore, I have created a query in which join added multiple times with condition. you can saw in below code section.

public function multidata(){	

	$this->db->join('table', ' =');
        $this->db->join('table2', ' =');
        $this->db->join('table4', ' ='); 
	$query = $this->db->get();
	return $query->result();



Finally, we describe whole ways to use join query in Codeigniter to retrieve data from the database.

As well as, if you have any issues regarding this ask us. Also, check this simple example of How to Store and Retrieve Image from database in Php


Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

Related Post

How to use Multiple Where Condition in Codeigniter?

Here, we displaying the best method to use multiple where condition in Codeigniter. Also, displaying another method get_where() and multiple where clause in Codeigniter. As well as, given an example fetch data from the database using multiple where condition. likewise, insert, update, and delete. It means all Codeigniter queries with used multiple where clause. Again, […]

Read more

Update Query in Codeigniter using Where Condition

Here, We displayed the best ways to update query in Codeigniter using where condition. Also, given an example of multiple where condition. Similarly, Codeigniter has to provide a simple query to replace data from the MySQL database using a clause. This is update query $this->db->replace(); Thus, I have to show an update function. This function […]

Read more

How to Delete Query with Where Condition in Codeigniter

I am showing the best method to learn of delete query with where condition in Codeigniter. As well as, explain Codeigniter delete query return value from the database. Also, given some example code of delete query. when using where condition in the below section. Delete Query with Where Condition in Codeigniter Here, We displaying a […]

Read more