phpcodeigniteractiverecordgrouping

How to stop parent table's record being repeated for every child record with LEFT JOIN in CodeIgniter query


I written query to get record from main table tpl_upload_csv_file and get related record from tbl_process_csv. I get all the records, but the problem is if I have 1 id from tpl_upload_csv_file in tbl_process_csv for 5 rows its displaying 5 times. Same record is displaying 5 times.

$this->db->select('tbl_process_csv.id, tbl_process_csv.record_no, tbl_process_csv.reason,tpl_upload_csv_file.uploaded_file_name, tpl_upload_csv_file.uploaded_date_time');
$this->db->from('tpl_upload_csv_file');
$this->db->where('tbl_process_csv.process_status', 3);
$this->db->join('tbl_process_csv', 'tbl_process_csv.csv_file_id = tpl_upload_csv_file.id', 'left');
$this->db->order_by('tbl_process_csv.date_of_processing', 'desc');
$query = $this->db->get();
print_r($query->result());die;
return $query->result();

My table structure is

tpl_upload_csv_file :

  • id
  • uploaded_file_name
  • uploaded_date_time
  • records_available

tbl_process_csv :

  • id
  • csv_file_id -->(Reference if for table tpl_upload_csv_file)
  • record_no
  • process_status
  • reason

Solution

  • Try this and let me know if any problem occur:

    $this->db->select('tbl_process_csv.id, tbl_process_csv.record_no, tbl_process_csv.reason,tpl_upload_csv_file.uploaded_file_name, tpl_upload_csv_file.uploaded_date_time');
    $this->db->from('tpl_upload_csv_file');
    $this->db->join('tbl_process_csv', 'tbl_process_csv.csv_file_id = tpl_upload_csv_file.id', 'left');
    $this->db->where('tbl_process_csv.process_status', 3);
    $this->db->group_by('tbl_process_csv.csv_file_id');
    $this->db->order_by('tbl_process_csv.date_of_processing', 'desc');
    $query = $this->db->get();
    return $query->result();