我编写了查询以从主表 tpl_upload_csv_file 获取记录,并从 tbl_process_csv 获取相关记录。我得到了所有记录,但问题是如果我在 tbl_process_csv 中有1个id tpl_upload_csv_file 5行,则显示5次。相同记录显示5次。
$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();
我的表结构是
tpl_upload_csv_file:
- ID
- uploaded_file_name
- uploaded_date_time
- records_available
tbl_process_csv:
- ID
- csv_file_id - >(如果表 tpl_upload_csv_file ,请参考
- record_no
- process_status
- 原因
答案 0 :(得分:1)
试试这个并告诉我是否有任何问题:
$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();
答案 1 :(得分:0)
$this->db->distinct('tbl_process_csv.id');
在代码中添加此行。
因此,您将获得不同的记录w.r.t tbl_process_csv.id