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

leonardeveloper

我有此日历,其中每天都有事件列表,并且使用此查询来计算特定日期的事件。在我的数据库中,我有start_timeandend_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
我使用服务器上的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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

正确使用COUNT计数SELECT语句中返回的行数

来自分类Dev

从select语句中的14个可能的列中返回日期

来自分类Dev

在语句中返回2个计数结果

来自分类Dev

NOT IN Select语句返回0值

来自分类Dev

在分组的SELECT语句中限制COUNT个?

来自分类Dev

预定义计数器未在select语句中更新

来自分类Dev

仅返回SQLite Select语句中的最后一个值

来自分类Dev

在一个SQL语句中连接多个select语句

来自分类Dev

如何设置列以在select语句中返回值

来自分类Dev

卡在简单的select语句中以返回单行

来自分类Dev

仅返回SQLite Select语句中的Last值

来自分类Dev

为什么在MySQL SELECT语句中甚至允许“ LIMIT 0”?

来自分类Dev

当多表的 select 语句中没有行时设置 0

来自分类Dev

SQL在select语句中合并两个表

来自分类Dev

在where子句中分配一个select语句

来自分类Dev

SQL在select语句中合并两个表

来自分类Dev

如何从该语句中的SELECT语句中减去两个结果?

来自分类Dev

在SELECT语句中绑定变量

来自分类Dev

select语句中的sql转换

来自分类Dev

Select语句中的列名无效

来自分类Dev

OData select语句中的别名

来自分类Dev

Select语句中的布尔逻辑

来自分类Dev

SELECT语句中的DATETIME函数

来自分类Dev

Select语句中的子查询

来自分类Dev

SELECT嵌套在WITH语句中

来自分类Dev

在select语句中设置变量?

来自分类Dev

WHERE子句中的Select语句

来自分类Dev

Select语句中的列名无效

来自分类Dev

SELECT语句中的sql concat