简体   繁体   English

多重加入codeigniter

[英]multiple join in codeigniter

Here am having three tables namely tbl_users , tbl_account_details , tbl_attendance .table tbl_users looks like this这里有三个表,即tbl_userstbl_account_detailstbl_attendance .table tbl_users看起来像这样

user_id     username            role_id     activated   banned  
    1        admin                  1           1         0         
    36       siraj                  3           1         0     
    37       faizal                 3           1         0     
    38       nesru                  3           1         0     
    40       jaseer                 3           1         0     
    42       maltu                  3           1         0     
    43       shahul                 3           1         0 

table tbl_account_details looks like thistbl_account_details看起来像这样

account_details_id  user_id     fullname        designations_id     
    1                 1       Administrator         1   
    33                36        siraj               2   
    34                37        faizal              3   
    35                38        nesru               3   
    37                40        jaseer              4   
    39                42        maltu               4   
    40                43        shahul              2   

my tbl_attendance table looks like this我的tbl_attendance表看起来像这样

attendance_id   user_id      date_in        attendance_status status 0=absent 1=present 3 = onleave 4 = onoff
    1            1          2017-02-05      1
    2            36         2017-02-11      4
    3            36         2017-02-11      4
    4            36         2017-02-11      3
    5            1          2017-02-02      1
    6            36         2017-02-01      1

i want to get all users where users is not in onleave and onoff on the current day in the tbl_attendance .the attendance status for leave is 3 and for off is 4. in order to get i wrote a query which looks like this我想所有users ,其中users是不是在onleaveonoff在当天的tbl_attendance请假.the出勤状况是3和关闭是4为了得到我写了一个查询,看起来像这样

public function get_manager_cordinator_users($date)
{

     $this->db->select('tbl_account_details.*', FALSE);
     $this->db->select('tbl_users.*');
     $this->db->join('tbl_account_details', 'tbl_users.user_id = tbl_account_details.user_id', 'left');
     $this->db->where(array('designations_id' => 2));
     $this->db->or_where(array('designations_id' => 3));
     $this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='.$date.' && attendance_status=3 OR attendance_status=4 )');
     $query_result = $this->db->get('tbl_users');
     $result = $query_result->result();
     return $result;


}

 my controller looks like this 


$date=date('Y-m-d');
$data['assign_user'] = $this->Project_model->get_manager_cordinator_users($date);

please help me to solve...请帮我解决...

I've created query which will return the data of users with designation_id 2 and 3, also which don't have attendance_status 3 and 4我创建了一个查询,它将返回具有 designation_id 2 和 3 的用户数据,这些用户也没有参与状态 3 和 4

Your controller code is fine -你的控制器代码很好 -

$date = date('Y-m-d');
$data['assign_user'] = $this->Project_model->get_manager_cordinator_users($date);

Just change your model code with below one -只需使用以下代码更改您的型号代码 -

public function get_manager_cordinator_users($date)
{
  $this->db->select( 'tbl_users.user_id, tbl_users.username,tbl_account_details.designations_id, tbl_attendance.date_in, tbl_attendance.attendance_status' );
  $this->db->from( 'tbl_users' );
  $this->db->join( 'tbl_account_details', 'tbl_account_details.user_id = tbl_users.user_id' );
  $this->db->join( 'tbl_attendance', 'tbl_attendance.user_id = tbl_users.user_id' );
  $this->db->where( 'tbl_attendance.date_in', $date );
  $this->db->where('tbl_users.banned', 0);
  $this->db->where('tbl_users.activated', 1);
  $this->db->where_in( 'tbl_account_details.designations_id', array( '2', '3' ) );
  $this->db->where_not_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
  return $this->db->get()->result();
}

It will provide you required user data.它将为您提供所需的用户数据。

now i got it现在我明白了

public function get_manager_cordinator_users()
{
 $date=date('Y-m-d');
 $this->db->select('tbl_users.*');
 $this->db->join('tbl_account_details', 'tbl_users.user_id = tbl_account_details.user_id', 'left');
 $this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in= "'.$date.'" AND tbl_attendance.attendance_status IN (3,4) ) ');

 $this->db->where_in( 'tbl_account_details.designations_id', array( '2','3' ) );
 $this->db->where('tbl_users.banned', 0);
 $this->db->where('tbl_users.activated', 1);

 $query_result = $this->db->get('tbl_users');
 $result = $query_result->result();
 return $result;



}

use this用这个

     $this->db->select('tbl_account_details.*', FALSE);
     $this->db->select('tbl_users.*');
     $this->db->join('tbl_users', 'tbl_users.user_id = tbl_account_details.user_id', 'left');
     $this->db->where('(designations_id=2 or designations_id=3 )');
     $this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='.$date.' && attendance_status=3 OR attendance_status=4 )');
     $query_result = $this->db->get('tbl_users');
     $result = $query_result->result();
     return $result;

$this->db->select('*)->from('tbl_users')->where('tbl_attendance.attendance_status <= 2')->where('tbl_attendance.data_in =',date('Ym-d '))->join('tbl_attendance','tbl_attendance.user_id = 'tbl_users.user_id','left')->get()->result();

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

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