我有此日历,其中每天都有事件列表,并且使用此查询来计算特定日期的事件。在我的数据库中,我有start_time
andend_time
字段,用户可以在其中指定具体的材料明细表。我select
当天尝试了这些事件,但查询中似乎出了点问题。因为用户可以借多于一种的材料,因此将被存储在具有相同的数据库start_time
和end_time
为好。我想要的是以相同的方式计算用户的所有数据,start_time
我尝试了一下,GROUP BY
但似乎也不起作用。这是我的数据库:
----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
在这里有一个很大的BUT,在该查询中它将为您提供此输出。注意:I'm using CodeIgniter
。
我使用服务器上的MySQL$this->output->enable_profiler(TRUE);
尝试日期2013-08-16
(因为它是多项选择),并给了我这个日期。
count | date_reserve | borrowerID | start_time | end_time
---------------------------------------------------------------------------------------------------
NULL | 2013-08-16 | bobi | 07:01:12 | 07:01:12
那么,您认为对此的解决方案是什么?
这是您要找的东西吗?
select
count(distinct concat(date_reserve, ' ', start_time)) as my_count,
date_reserve,borrowerID,start_time,end_time
from
schedule
where borrowerID = 'bobi'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句