0

嘿伙计们,我有一个 mysql 表,其中包含一些记录,如下图所示!

date_time 字段具有 datetime 数据类型

我有以下查询来计算指定时间范围内的记录数(sr13、r4 和华为的记录数)。

$date1 = "2012-01-02 03";
$date2 = "2014-12-30 24";
$counter_result = $this->db->query("select count(sr13) as count_sr13, count(r4) as count_r4, count(huawei) as count_huawei from cdrcount where date_time BETWEEN '$date1%' and '$date2%' ");

查询将三个字段的值返回为 0。查询有什么问题吗?

4

3 回答 3

0

试用time_to_sec功能

select count(sr13) as count_sr13, count(r4) as
count_r4, count(huawei) as count_huawei from cdrcount 
where time_to_sec(date_time) 
BETWEEN time_to_sec('date1') and time_to_sec('date2');
于 2013-03-11T08:29:29.700 回答
0

要正确比较日期,您应该使用以下完整值:

$date1 = "2012-01-02 03:00:00";
$date2 = "2014-12-30 23:59:59";

%并从查询中删除:

$counter_result = $this->db->query("select count(sr13) as count_sr13, count(r4) as count_r4, count(huawei) as count_huawei 
    from cdrcount 
    where date_time BETWEEN '$date1' and '$date2'");
于 2013-03-11T08:31:05.767 回答
0

原因是因为您在contains 中date_time的值被隐式转换为字符串。因此,它将字符串数据类型中的日期与包含并且显然没有匹配记录的值进行比较,尝试删除它,BETWEEN%%

select  count(sr13) as count_sr13, 
        count(r4) as count_r4, 
        count(huawei) as count_huawei 
from    cdrcount 
where   date_time BETWEEN '$date1' and '$date2'

并且您的日期应格式化为有效日期,例如,2012-01-02 03:00:00

于 2013-03-11T08:28:14.050 回答