php - 在 codeigniter 中多次返回相同的结果

标签 php mysql codeigniter codeigniter-3

[我想显示特定员工的具体奖励数据,但它在多次重复相同的结果 模型函数在这里 公共(public)函数 get_all_awards($id = NULL) {

    $this->db->select('tbl_employee_award.*', FALSE);
    $this->db->select('tbl_employee.*', FALSE);
    $this->db->select('tbl_designations.*', FALSE);
    $this->db->select('tbl_department.department_name', FALSE);
    $this->db->from('tbl_employee_award');
    $this->db->join('tbl_employee', 'tbl_employee_award.employee_id  = tbl_employee.employee_id', 'left');
    $this->db->join('tbl_designations', 'tbl_designations.designations_id  = tbl_employee.designations_id', 'left');
    $this->db->join('tbl_department', 'tbl_department.department_id  = tbl_designations.department_id', 'left');
    $this->db->order_by("award_date", "desc");
    if (!empty($id)) {
        // var_dump($id);
        //die();
        $this->db->where('tbl_employee_award.employee_id',$id);
        $query_result = $this->db->get();

        $result = $query_result->row();

    } else {
        $query_result = $this->db->get();
        $result = $query_result->result();
    }
   // var_dump( $result->employee_id);
   // die();
    return $result;

}][1]

最佳答案

您必须将基表设置为员工表。因为您需要根据员工而不是奖项提供数据。

public function get_all_awards($id = NULL)
{
    $this->db->select("E.id, E.employee_name, A.award_names, DES.designation,  D.department_name");
    $this->db->from('tbl_employee as E');
    $this->db->join('tbl_employee_award as A', 'A.employee_id = E.employee_id', 'left');
    $this->db->join('tbl_designations as DES', 'DES.designations_id  = E.designations_id', 'left');
    $this->db->join('tbl_department as D', 'D.department_id  = E.department_id', 'left');
    $this->db->order_by("A.award_date", "desc");    
    if($id !== NULL)
    {
        $this->db->where('E.employee_id', $id);
    }

    $query = $this->db->get();

    $result = $query->result();
    print_r($result);
    die;
}

在上面的查询中,如果您为员工授予单一奖项,那么您将在结果中得到单行。如果一名员工赢得了多个奖项,那么它将返回同一员工但不同奖项的多行。如果任何员工两次获奖,那么您也获得两次记录。

关于php - 在 codeigniter 中多次返回相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44277596/

相关文章:

带有html代码的变量中的php代码

javascript - jQuery 各种字段添加以及带有 JSFiddle 的可选复选框

mysql - 更新时间间隔内第一次出现的值

php - MySQL查找2个日期是否在多行的日期范围之间

php - mysql中if语句的使用方法

php - 在 PHP 中自动检测 360 度自然图像

php - 在 where 子句中选择具有长比较的记录的最佳技术是什么

java - 由 : com. mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException 引起

javascript - Summernote 图像上传在 codeigniter 中不起作用

php - Codeigniter 按钮重定向