簡體   English   中英

SELECT語句中的SELECT返回0個計數

[英]SELECT inside SELECT statement returns 0 count

我有此日歷,其中每天都有事件列表,我使用此查詢來計算特定日期的事件。 在我的數據庫中,我有start_timeend_time字段,用戶可以在其中指定具體的材料明細表。 我嘗試select當天的事件,但查詢中似乎出了點問題。 因為用戶可以借用一種以上的材料,所以它們也將以相同的start_timeend_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM