Codeigniter Join Example

This tutorial will show you how we can use Codeigniter’s Query Builder Class to build join queries for fetching data from multiple tables.

The SQL Join clause is used to combine records together from two or more tables in a database. A JOIN locates related column values in two or more tables. This will display all the records that have matched. Join and Inner Join are functionally equivalent, but INNER JOIN can be a bit clearer to read, especially if the query has other join types (i.e. LEFT or RIGHT or CROSS) included in it. Here we will see Join or Inner Join example.

We will use Codeigniter’s $this->db->join() that permits you to write the JOIN portion of your query.

Read also Codeigniter Full Outer Join Example ,  Codeigniter Right Outer Join Example and Codeigniter Left Outer Join Example


Netbeans 8.1
XAMPP in Windows
Codeigniter 3.0.6

Configure XAMPP and Netbeans

From Netbeans IDE go to Tools->Options. Click on PHP. Now on tab “General” browse the file for “PHP 5 Interpreter”. The php interpreter file generally placed inside the <physical drive in Windows OS>:xamppphpphp.exe

Configure Codeigniter and Netbeans

Create a new PHP project in Netbeans. Then remove the index.php file from the newly created project. Now copy the extracted files from Codeigniter 3.0.6 to the newly created project directory.

Example on Join Query

Suppose we have blogs table with below data

sql join

And we have blog_comments table with below data

sql join

On executing below query


We will get below results

join query result

Directory Structure

A typical directory structure for the project would be as shown below. Here assests directory will contain static resources like css, js, images.
codeigniter join

Step 1. Now modify <root directory>/application/config/autoload.php file for auto-loading html, url, file, form and session

Step 2. Now modify <root directory>/application/config/config.php file to define encryption key

Step 3. Create a view file under <root directory>/application/views which will be used to display blogs

Step 4. Create a Controller class under <root directory>/application/controllers for handling client’s request and response

Step 5. Create a Model class under <root directory>/application/models for performing database operatins

Step 6. Now modify <root directory>/application/config/routes.php file for pointing the default controller class

Step 7. Now if everything is fine run the application, you will see below output in the browser

codeigniter join

Download MySQL script


Thanks for reading.


Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.