[英]SELECT inside SELECT statement returns 0 count
我有此日历,其中每天都有事件列表,我使用此查询来计算特定日期的事件。 在我的数据库中,我有start_time
和end_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
。
我用$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
那么您认为对此的解决方案是什么?
这是您要找的东西吗?
select
count(distinct concat(date_reserve, ' ', start_time)) as my_count,
date_reserve,borrowerID,start_time,end_time
from
schedule
where borrowerID = 'bobi'
基于fancyPants。 查询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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.