在代码点火器中多次返回相同的结果

时间:2017-05-31 06:49:47

标签: php mysql codeigniter codeigniter-3

[我想显示特定员工的具体奖励数据,但在多个时间点会产生相同的结果 模型功能在这里    public function 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]

1 个答案:

答案 0 :(得分:0)

您必须将基表设置为Employee表。因为您需要根据员工而不是奖励的数据。

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;
}

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