Joins in Codeigniter - ONS TECH Global

18 May 2016
Comments  0

In this article i will explain how a Code-igniter join works, and how it is applied like a basic join of PHP.

This is the basic join query where we select data from the main table and than apply additional joins based on the foreign and primary key.

$this->db->select(‘*’);
$this->db->from(‘TableA’);
$this->db->join(‘TableC’, ‘TableA.COLUMN_NAME_TABLE_A = TableC.COLUMN_NAME_TABLE_C’, ‘INNER’);
$this->db->join(‘TableB’, ‘TableB.COLUMN_NAME_TABLE_B = TableC.COLUMN_NAME_TABLE_C’, ‘Left’);
$result = $this->db->get()->result();

—————————————————————————-

So here you can see that, $this->db->join() is used for applying a code-igniter join. Here JOIN() is a parent function of CI which is defined in the code-igniter parent classes. We have to follow a certain procedure to use it, which i am gonna explain now.

The first parameter contains the table name from where the data needs to be fetched using join. The second parameter is the column name of the main table to which join is applied & the column name of the Fetched table to which join is applied. The last or the 3rd parameter is the type of join applied.

So this is a simple way to apply joins in CI. In case you want to have an confusions , feel free to ask us anytime.

Leave a Reply

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