gpt4 book ai didi

php - MySQL join显示多条记录

转载 作者:行者123 更新时间:2023-11-29 02:25:05 27 4
gpt4 key购买 nike

我正在使用 CodeIgniter,在我的 sql 连接查询中它获得了重复值。例如,如果 tbl_employee_contact 有两个联系电话,它会显示相同的记录两次,每次显示不同的联系电话。如何只显示一条记录?

Database image

Image example

这是我的模型

function get_records(){
$this->db->select(array(
'tbl_employee_registration.emp_id',
'tbl_employee_registration.emp_fname',
'tbl_employee_registration.emp_email',
'tbl_employee_registration.emp_status',
'tbl_employee_contact.emp_contact',
));
$this->db->from('tbl_employee_registration');
$this->db->join('tbl_employee_contact','tbl_employee_contact.emp_id=tbl_employee_registration.emp_id');

$query = $this->db->get();
return $query->result();
}

这是我的 Controller

function manage_operators(){
$data = array();
if($query = $this->mod_employee->get_records())
{
$data['records'] = $query;
}
$this->load->view('admin/admin_manage_operators',$data);
}

最佳答案

你可以使用group_by

 $this->db->group_by("your table");

最后你的模型看起来像这样

 function get_records(){
$this->db->select(array(
'tbl_employee_registration.emp_id',
'tbl_employee_registration.emp_fname',
'tbl_employee_registration.emp_email',
'tbl_employee_registration.emp_status',
'tbl_employee_contact.emp_contact',
));
$this->db->from('tbl_employee_registration');
$this->db->join('tbl_employee_contact','tbl_employee_contact.emp_id=tbl_employee_registration.emp_id');
$this->db->group_by("tbl_employee_registration.emp_id");

$query = $this->db->get();
return $query->result();
}

关于php - MySQL join显示多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23936078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com