简体   繁体   English

SELECT语句中的SELECT返回0个计数

[英]SELECT inside SELECT statement returns 0 count

I have this calendar where there is a list of events in every day and I use this query to count the events in a particular date. 我有此日历,其中每天都有事件列表,我使用此查询来计算特定日期的事件。 In my database I have start_time and end_time field where the user have a schedule of materials to be specific. 在我的数据库中,我有start_timeend_time字段,用户可以在其中指定具体的材料明细表。 I tried to select the events in that day but it seems there's something wrong in my query. 我尝试select当天的事件,但查询中似乎出了点问题。 Because the user can borrow more than one material so it will be stored in the database with the same start_time and end_time as well. 因为用户可以借用一种以上的材料,所以它们也将以相同的start_timeend_time存储在数据库中。 What I want is to count all the data of the user with the same start_time and I tried GROUP BY but it seems doesn't work either. 我想要的是使用相同的start_time来计数用户的所有数据,我尝试了GROUP BY但似乎也不起作用。 Here is my database: 这是我的数据库:

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

Here is my query: 这是我的查询:

    $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)' : '';
        }
}   

So the expected output with the will be: 因此, 预期的输出为:

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

In here there's a big BUT where in that query it will give you this output. 在这里有一个很大的BUT ,在该查询中它将为您提供此输出。 Note: I'm using CodeIgniter . 注意: I'm using CodeIgniter
I used $this->output->enable_profiler(TRUE); 我用$this->output->enable_profiler(TRUE); and try the date 2013-08-16 (because it is multiple selection) to MySQL on my server and give me this. 并尝试在我的服务器上将日期2013-08-16 (因为它是多项选择)发送到MySQL,然后给我。

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

So what do you think the solution for this? 那么您认为对此的解决方案是什么?

Is this what you're looking for? 这是您要找的东西吗?

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

Based on fancyPants. 基于fancyPants。 Query for CI: 查询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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM