How to join multiple column using codeigniter?

Member

by gilbert , in category: PHP Frameworks , 5 months ago

How to join multiple column using codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , 5 months ago

@gilbert 

In CodeIgniter, you can join multiple columns from different tables by using the join() method in the query builder class. Here's an example of how to join multiple columns in CodeIgniter:


Assuming you have two tables, table1 and table2, and you want to join them on multiple columns column1 and column2:

1
2
3
4
5
6
$this->db->select('table1.column1, table1.column2, table2.column3, table2.column4');
$this->db->from('table1');
$this->db->join('table2', 'table1.column1 = table2.column3 AND table1.column2 = table2.column4', 'inner');
$query = $this->db->get();

$result = $query->result();


In this example, we first select the columns we want to retrieve from both tables using the select() method. Then, we specify the tables we want to join using the from() method. Finally, we use the join() method to join the tables on the specified columns.


You can also use different types of joins such as left, right, or outer by specifying the join type as the third parameter in the join() method.


Remember to replace table1, table2, column1, column2, column3, and column4 with your actual table and column names.