简体   繁体   English

如何在Codeigniter中使用Join获取两个表的数据

[英]How to get data of both table using join in codeigniter

I have two array 我有两个数组

 Array
(
    [0] => stdClass Object
        (
            [id] => 14
            [employee_id] => ST0011
            [emp_name] => Munish Sharma
            [gender] => 
            [marital_status] => 
            [address] => 
            [postal_code] => 
            [home_phone] => 
            [mobile_phone] => 
            [work_email] => 
            [private_email] => 
            [joined_date] => 1970-01-01 00:00:00
            [emp_salary] => 0
            [confirmation_date] => 0000-00-00 00:00:00
            [department] => HRM
            [f_name] => fname
            [dob] => 1970-01-01
            [c_address] => 
            [domainName] => 
            [emp_status] => active
        )

    [1] => stdClass Object
        (
            [id] => 12
            [employee_id] => ST001dasd
            [emp_name] => Rakesh Negi
            [gender] => 
            [marital_status] => 
            [address] => 
            [postal_code] => 
            [home_phone] => 
            [mobile_phone] => 
            [work_email] => 
            [private_email] => 
            [joined_date] => 2015-08-11 00:00:00
            [emp_salary] => 0
            [confirmation_date] => 0000-00-00 00:00:00
            [department] => HRM
            [f_name] => dsad
            [dob] => 1970-01-01
            [c_address] => 
            [domainName] => 
            [emp_status] => active
        )

    [2] => stdClass Object
        (
            [id] => 13
            [employee_id] => ST001
            [emp_name] => Rakesh Negi
            [gender] => 
            [marital_status] => 
            [address] => 
            [postal_code] => 
            [home_phone] => 
            [mobile_phone] => 
            [work_email] => 
            [private_email] => 
            [joined_date] => 2015-08-06 00:00:00
            [emp_salary] => 0
            [confirmation_date] => 0000-00-00 00:00:00
            [department] => HRM
            [f_name] => Father Name
            [dob] => 1970-01-01
            [c_address] => 
            [domainName] => 
            [emp_status] => active
        )

)

and the second array is this 第二个数组是这个

Array 数组

(
    [0] => stdClass Object
        (
            [id] => 55
            [emp_id] => ST001
            [check_in] => 11:38:09
            [check_out] => 00:00:00
            [total_time] => 00:00:00
            [status] => 
            [date_time] => 2015-10-20
            [emp_name] => Rakesh Negi
        )

)

Now I want to make it like this 现在我要像这样

Array
    (
        [0] => stdClass Object
            (
                [id] => 14
                [employee_id] => ST0011
                [emp_name] => Munish Sharma
                [gender] => 
                [marital_status] => 
                [address] => 
                [postal_code] => 
                [home_phone] => 
                [mobile_phone] => 
                [work_email] => 
                [private_email] => 
                [joined_date] => 1970-01-01 00:00:00
                [emp_salary] => 0
                [confirmation_date] => 0000-00-00 00:00:00
                [department] => HRM
                [f_name] => fname
                [dob] => 1970-01-01
                [c_address] => 
                [domainName] => 
                [emp_status] => active
            )

        [1] => stdClass Object
            (
                [id] => 12
                [employee_id] => ST001dasd
                [emp_name] => Rakesh Negi
                [gender] => 
                [marital_status] => 
                [address] => 
                [postal_code] => 
                [home_phone] => 
                [mobile_phone] => 
                [work_email] => 
                [private_email] => 
                [joined_date] => 2015-08-11 00:00:00
                [emp_salary] => 0
                [confirmation_date] => 0000-00-00 00:00:00
                [department] => HRM
                [f_name] => dsad
                [dob] => 1970-01-01
                [c_address] => 
                [domainName] => 
                [emp_status] => active
            )

         [3] => stdClass Object
            (
                [id] => 55
                [emp_id] => ST001
                [check_in] => 11:38:09
                [check_out] => 00:00:00
                [total_time] => 00:00:00
                [status] => 
                [date_time] => 2015-10-20
                [emp_name] => Rakesh Negi
            )

)

I want to remove the value from first array if the em_id in the both array will be same is it possible in? 如果两个数组中的em_id相同,我想从第一个数组中删除该值吗?

I tried using where condion with join like this 我试过像这样在哪里使用条件

$date = date('Y-m-d');
            $this->db->select('hrm_attendance.*,employees.emp_name');
            $this->db->from('hrm_attendance');
            $this->db->join('employees','hrm_attendance.emp_id=employees.employee_id','LEFT OUTER');            
            $this->db->where('hrm_attendance.date_time = "'.$date.'"');
            $this->db->order_by('check_in','asc');
            $query = $this->db->get();
            return $query->result();

and getting this output 并获得此输出

(
    [0] => stdClass Object
        (
            [id] => 55
            [emp_id] => ST001
            [check_in] => 11:38:09
            [check_out] => 00:00:00
            [total_time] => 00:00:00
            [status] => 
            [date_time] => 2015-10-20
            [emp_name] => Rakesh Negi
        )

)

But I want the output I have described above 但是我想要上面已经描述的输出

if I remove $this->db->where('hrm_attendance.date_time = "'.$date.'"'); 如果我删除$this->db->where('hrm_attendance.date_time = "'.$date.'"'); line then I am getting the all record from both the table I want all the data from the employee table with all ID and only those data from the hrm_attendance where the emp_id is available with where condition $this->db->where('hrm_attendance.date_time = "'.$date.'"'); 行,然后从表中获取所有记录,我希望从employee表中获取所有具有所有ID的数据,并且仅从hrm_attendance中获取那些emp_id可用的数据,其中条件$this->db->where('hrm_attendance.date_time = "'.$date.'"');

Please check the Code igniter Docs 请检查代码点火器文档

http://www.codeigniter.com/userguide2/database/active_record.html#select http://www.codeigniter.com/userguide2/database/active_record.html#select

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');

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

// Produces: 
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id

in select('*') you can specify your limited fields also. 在select('*')中,您还可以指定您的有限字段。

In your db->where is incorrect $this->db->where('hrm_attendance.date_time = "'.$date.'"'); 在您的db->where不正确的$this->db->where('hrm_attendance.date_time = "'.$date.'"');

http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data

public function some_name() {

   $date = date('Y-m-d');

   $this->db->select('*');
   $this->db->from($this->db->dbprefix . 'hrm_attendance ha', 'LEFT');           
   $this->db->join($this->db->dbprefix . 'employees e', 'e.employee_id = ha.emp_id', 'LEFT');            
   $this->db->where('ha.date_time', $date);
   $this->db->order_by('check_in','ASC');
   $query = $this->db->get();

   return $query->result();

   // Or Try With result_array();

   // return $query->result_array();

}

I find out the solution by adding some code like this 我通过添加这样的代码找出解决方案

            $date = date('Y-m-d');
            $this->db->select('hrm_attendance.*,employees.emp_name');
            $this->db->from('hrm_attendance');
            $this->db->join('employees','hrm_attendance.emp_id=employees.employee_id','LEFT OUTER');            
            $this->db->where('hrm_attendance.date_time = "'.$date.'"');
            $this->db->order_by('check_in','asc');
            $query = $this->db->get();
            $result = $query->result();
      $count = count($result);
       for($i = 0;$i<$count;$i++)
        {
               $id[] = $data['user_logged_in'][$i]->emp_id;
        }
            $this->db->select('*');
            $this->db->from('employees');
            $this->db->where_not_in('employee_id',$id);
            $query = $this->db->get();
            $data['absent'] = $query->result();
            print_r($data['absent']);

By using this I got the all those employee who are not in the table hrm_attendance 通过使用此方法,我得到了所有不在表hrm_attendance中的员工

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM