2013-08-20 83 views
1

我有这个日历,每天都有一个事件列表,我使用这个查询来计算特定日期的事件。在我的数据库中,我有start_timeend_time字段,其​​中用户具有特定材料的时间表。我尝试了select当天的事件,但似乎在我的查询中出现了问题。因为用户可以借用多个材料,所以它将以相同的start_timeend_time存储在数据库中。我想要的是用相同的start_time来统计用户的所有数据,并且我尝试了GROUP BY,但它似乎也不起作用。这里是我的数据库:SELECT语句中的SELECT返回0计数

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

这里是我的查询:

$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 

在这里有一个大的在那个查询中它会给你这个输出。 备注: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'的事务。对不起,我的英文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');