2013-08-20 3 views
1

나는이 일정에 매일 이벤트 목록이 있고이 쿼리를 사용하여 특정 날짜의 이벤트를 계산합니다. 내 데이터베이스에는 start_timeend_time 필드가 있는데 여기에는 사용자가 구체적으로 자료의 일정을 가지고 있습니다. 나는 그 날의 사건을 select에 시도했다. 그러나 나의 질문에 뭔가 잘못된 것 같다. 사용자는 둘 이상의 자료를 빌릴 수 있으므로 데이타베이스에 동일한 start_timeend_time과 함께 저장됩니다. 내가 원하는 것은 start_time과 같은 사용자의 모든 데이터를 계산하는 것이며 GROUP BY을 시도했지만 작동하지 않는 것 같습니다.SELECT SELECT 문에서 0이 반환됩니다.

$cal_data = array(); 
    for($i=1;$i<=31;$i++) 
    { 
    $date = "$year-$month-$i"; 
    $this->db->select('(SELECT COUNT(id) as count_s FROM schedule WHERE date_reserve='.$date.' GROUP BY start_time) as count', FALSE); 
    $this->db->select('date_reserve,borrowerID,start_time,end_time'); 
    $this->db->from('schedule');  
    $this->db->where('date_reserve',"$year-$month-$i"); 
    $this->db->group_by('start_time'); 
    $query = $this->db->get(); 

     foreach ($query->result() as $row) { 

      $cal_data[$i] = ($row->count > 0) ? $row->count.' ' .'event(s)' : ''; 
     } 
} 

그래서 함께 예상 출력은 다음과 같습니다 : 여기에서

count | date_reserve | borrowerID | start_time | end_time 
    --------------------------------------------------------------------------------------------------- 
     1  |  2013-08-16  |  bobi  | 07:01:12 | 07:01:12 

하지만있다 여기

----Table: schedule---- 
    id | materialID | borrowerID | date_reserve | start_time | end_time| 
    ----------------------------------------------------------------------------------- 
    9  | 7   | bobi  | 2013-08-16 | 07:01:12 | 07:01:12|  
    10 | 10   | bobi  | 2013-08-16 | 07:01:12 | 07:01:12| 
    11 | 12   | bobi  | 2013-08-16 | 07:01:12 | 07:01:12| 
    12 | 7   | sobi  | 2013-08-18 | 07:01:12 | 07:01:12| 
    ------------------------------------------------------------------------------------ 

내 쿼리입니다 : 여기 내 데이터베이스입니다 그 쿼리에서 당신에게이 결과를 줄 것입니다. 참고 : I'm using CodeIgniter.
$this->output->enable_profiler(TRUE);을 사용하고 2013-08-16 (여러 선택이기 때문에) 서버에있는 MySQL에이 날짜를 알려 주시고 저에게 알려주세요.

count | date_reserve | borrowerID | start_time | end_time 
    --------------------------------------------------------------------------------------------------- 
    NULL |  2013-08-16  |  bobi  | 07:01:12 | 07:01:12 

그래서 해결책이 무엇이라고 생각하십니까?

+0

나는 힘든 시간을 당신이 원하는 것을 이해합니다. 특히 나는 예상 한 결과물과 당신의 질문에 당신의 말을 맞추는데 어려움이 있습니다. 왜 카운트가 1이되어야합니까? 질문을 바꿔 주시겠습니까? – fancyPants

+0

이유 1? 이는 동일한 'start_time'을 가진 1 개의 트랜잭션이기 때문입니다. 미안 내 영어 BTW – leonardeveloper

+0

@ fancyPants 당신이 마음에 드시겠습니까? https://chatstep.com/#leonard – leonardeveloper

답변

1

이게 당신이 찾고 있는게 있나요?

select 
count(distinct concat(date_reserve, ' ', start_time)) as my_count, 
date_reserve,borrowerID,start_time,end_time 
from 
schedule 
where borrowerID = 'bobi' 
+0

바이올린을 열 수 없습니다. – leonardeveloper

1

에서 작업을 참조하십시오. CI에 대한 쿼리 :

$this->db->select('count(distinct concat(date_reserve, " ", start_time)) as my_count)', FALSE); 
$this->db->select('date_reserve,borrowerID,start_time,end_time');