如何组合两个查询以在codigniter中生成一个结果

时间:2016-11-15 07:37:42

标签: php

这是我的查询工作正常

      $offset = ($this->uri->segment(4))?$this->uri->segment(4):0;
        $limit =10;

我没有使用$ user_id becoz此功能使用分页

   $q = "select distinct attandence.user_id ,monthname(date), sum(is_absent) as absents,in_time,out_time , count(date)  as working_days, leave_type , users.f_name ,leave_types.type ,leave_types.leave_type_id , users.l_name ,( select count(leave_type) from attandence where leave_type != 0 and user_id = 4 and month(date) = 10 ) as leave_count  from attandence join users on (users.user_id = attandence.user_id) left join leave_types on(leave_types.leave_type_id = attandence.leave_type) where in_time != '' and out_time != ''  ".$where_condition." GROUP BY attandence.user_id LIMIT ".$limit." OFFSET ". $offset; 

     $rows = $this->attandence_model->q($q);

     $this->data['Details'] = $rows;

在视图中我刚刚通过了

  <? foreach ($Details as $detail){ ?>
  <? } ?>





   Any help is going to be appreciated. :)

1 个答案:

答案 0 :(得分:0)

我有解决方案您的查询可以添加子查询Sub query

参见示例:

$q = "select distinct attandence.user_id ,monthname(date), sum(is_absent) as absents,in_time,out_time , count(date)  as working_days, leave_type , users.f_name ,leave_types.type ,leave_types.leave_type_id , users.l_name, (select count(leave_type) from attandence where leave_type != 0 and user_id = 15 and month(date) = 10) as leave_count  from attandence join users on (users.user_id = attandence.user_id) left join leave_types on(leave_types.leave_type_id = attandence.leave_type) where in_time != '' and out_time != ''  ".$where_condition." GROUP BY attandence.user_id LIMIT ".$limit." OFFSET ". $offset;