我有这个日历,每天都有一个事件列表,我使用这个查询来计算特定日期的事件。在我的数据库中,我有start_time
和end_time
字段,其中用户具有特定材料的时间表。我尝试了select
当天的事件,但似乎在我的查询中出现了问题。因为用户可以借用多个材料,所以它将以相同的start_time
和end_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
那么你认为这个解决方案是什么?
我很难理解你想要什么。特别是我有麻烦匹配您的话与您的预期输出和您的查询。为什么计数应该是1?你能否重新说明这个问题? – fancyPants
为什么1?因为它是一个具有相同'start_time'的事务。对不起,我的英文BTW – leonardeveloper
@fancyPants你介意吗? https://chatstep.com/#leonard – leonardeveloper