Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL连接显示多条记录_Php_Mysql_Sql_Codeigniter - Fatal编程技术网

Php MySQL连接显示多条记录

Php MySQL连接显示多条记录,php,mysql,sql,codeigniter,Php,Mysql,Sql,Codeigniter,我使用的是CodeIgniter,在我的sql连接查询中,它会得到重复的值。例如,如果tbl\u employee\u contact有两个联系人号码,它会显示两次相同的记录,每个记录的联系人号码不同。如何仅显示一条记录 这是我的模型 function get_records(){ $this->db->select(array( 'tbl_employee_registration.emp_id', 'tbl_employee_reg

我使用的是CodeIgniter,在我的sql连接查询中,它会得到重复的值。例如,如果
tbl\u employee\u contact
有两个联系人号码,它会显示两次相同的记录,每个记录的联系人号码不同。如何仅显示一条记录

这是我的模型

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();
}
这是我的控制器

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

您需要所谓的
分组依据
或将
不同的
添加到select查询中

$this->db->select(array(
    'DISTINCT  tbl_employee_registration.emp_id',
    'DISTINCT  tbl_employee_registration.emp_fname',
    'DISTINCT  tbl_employee_registration.emp_email',
    'DISTINCT  tbl_employee_registration.emp_status',
    'DISTINCT  tbl_employee_contact.emp_contact',    
));
或者,您可以选择所有数据,但在循环中,只需添加到具有唯一ID的数组中,如


$arr[$ID][]=$record

您可以使用
group\u 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();
    }

这不是一个代码点火器问题,从技术上讲,这根本不是一个问题。这就是SQL联接的工作方式。如果您在一对多关系上执行联接,那么您将获得一个表的“副本”,以及联接表中所有不同的可能性。您是否可以包括一个导致问题的tbl_employee_注册和tbl_employee_联系人的示例?是的,看起来像是一对多关系,因此显示多行。那么,您希望如何显示数据,您将保留哪一行,以及哪一行将从选择中删除?@Abhik Chakraborty这并不重要。任何row@Scott我在帖子中添加了数据库图像