codeigniter中的多个连接

时间:2017-02-11 05:56:16

标签: php mysql codeigniter

这里有三个表格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 

tbl_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   

我的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

我希望在users当天users onleaveonoff之内的所有tbl_attendance得到3。是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); ,关闭是4。 为了让我写了一个看起来像这样的查询

{{1}}

请帮我解决......

4 个答案:

答案 0 :(得分:1)

我已经创建了一个查询,它将返回具有designation_id 2和3的用户数据,也没有出勤率_status 3和4

您的控制器代码很好 -

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

只需更改下面的模型代码 -

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

它将为您提供所需的用户数据。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

现在我明白了

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;



}

答案 3 :(得分:0)

使用此

http://localhost/moviesearch/action/movies.html