Search code examples
mysqlcodeigniterinner-join

Getting table specific columns after joining the tables in CodeIgniter


I've 3 tables lets say A, B, and C. And they have common fields created_date which are table specific.

If I join the tables with left join using id's, then how can I get like, A.created_date after fetching the records in the view page while displaying the result. I mean if I use $row->created_date it will fetch me some other tables records or 01-01-1970 which is useless.


Solution

  • I was wondering why the desired result was not displayed. I'm thankful to @naim malek for the clue. Tried searching more solutions from the web. This worked for me.

    $query_selection = "a.created_date as first_created_date, a.userid as first_userid, b.accountid as sec_accountid";
    $data = array('a.userid'=>$userid);
    
    $this->db->select($query_selection);
    $this->db->from($table); 
    if($data!=NULL){
        $this->db->where($data);
    }
    $result_set = $this->db->get();
    if($result_set->num_rows() > 0){
        return $result_set->result();
    }
    else {
        return FALSE;
    }