CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New
Need a Website Or Web Application Contact : +91 9437911966 (Whatsapp) Note: Paid Service

How to use joins in codeIgniter


Normal Join

$this->db->select('*');
$this->db->from('articles');
$this->db->join('category', 'category.id = articles.id');
 
$query = $this->db->get();
 
/* Produces: */
/* SELECT * FROM articles JOIN category ON category.id = articles.id */

Join With Condition

$this->db->select('*');
$this->db->from('articles');
$this->db->join('category', 'category.id = articles.id');
$this->db->where(array('category.id' => 10));
 
$query = $this->db->get();
 
/* Produces: */
/* SELECT * FROM articles JOIN category ON category.id = articles.id where category.id = 10 */

Join with multiple tables

$this->db->select('*');
$this->db->from('articles');
$this->db->join('category', 'category.id = articles.id');
$this->db->join('comments', 'comments.id = articles.id');
 
$query = $this->db->get();
 
/* Produces: */
/* SELECT * FROM articles */
/* JOIN category ON category.id = articles.id */
/* JOIN comments ON comments.id = articles.id */

Join with specific type (left, right, outer, inner, left outer, and right outer)

$this->db->select('*');
$this->db->from('articles');
$this->db->join('category', 'category.id = articles.id','left');
$this->db->join('comments', 'comments.id = articles.id','left');
 
$query = $this->db->get();
 
/* Produces: */
/* SELECT * FROM articles */
/* LEFT JOIN category ON category.id = articles.id */
/* LEFT JOIN comments ON comments.id = articles.id */