codeigniter Query Structure Join Tables Using Query Builder


Example

Sometimes we need to join multiple tables to get aggregate data in return. here is how we can achieve the same using CodeIgniter Query Builder / Active Records.

public function getStudentInfo($studentid){
    $query = $this->db->select("st.id, st.name, st.class, mk.maths, mk.science")
               ->from("students as st")
               ->join("marks as mk", "mk.student_id = st.id", "inner")
               ->where("st.id", $studentId)
               ->get();
    return $query->result();        
}

Here we use join() to join multiple tables and we can change join type in 3rd parameter like "inner", "left", "right" etc.