How to join table in Codeigniter?

by raven_corwin , in category: PHP Frameworks , 9 months ago

How to join table in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , 3 months ago

@raven_corwin 

To join two tables in Codeigniter, you can use the join method of the database query builder class. Here is an example of how to use it:

1
2
3
4
5
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.field1 = table2.field2');
$query = $this->db->get();
return $query->result();


This will generate an inner join SQL query that selects all fields from both table1 and table2, and returns the result set.


You can also specify additional join conditions and use different types of joins, such as left join, right join, or outer join. Here is an example of a left join:

1
2
3
4
5
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.field1 = table2.field2', 'left');
$query = $this->db->get();
return $query->result();


For more information about joining tables in Codeigniter, you can refer to the official documentation: https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data