查询在PHP中不起作用,但直接在MySQL中起作用

时间:2016-03-01 04:57:37

标签: php mysql laravel laravel-4

当我尝试这个Eloquent查询时,我得到0作为结果,当我期待217时:

OrderItem::whereBetween(
        'created_at',
        [
            DB::raw("CONVERT_TZ('$start_date 09:00:00', '+00:00', '-06:00')"),
            DB::raw("CONVERT_TZ('$end_date 18:00:00', '+00:00', '-06:00')")
        ]
    )->count();

如果我在上面做DB::getQueryLog(),请复制已编译的查询以使用MySQL Workbench在数据库上执行它,然后我得到217的预期结果。

select count(*) as aggregate 
from `order_items`
where
`created_at` between
CONVERT_TZ('2016-02-17 09:00:00', '+00:00', '-06:00') and
CONVERT_TZ('2016-02-29 18:00:00', '+00:00', '-06:00')

以下是该查询日志的var转储:

array (3) [
    'query' => string (82) "select count(*) as aggregate from `order_items` where `created_at` between ? and ?"
    'bindings' => array (2) [
        Illuminate\Database\Query\Expression (1) (
            protected value -> string (53) "CONVERT_TZ('2016-02-17 09:00:00', '+00:00', '-06:00')"
        )
        Illuminate\Database\Query\Expression (1) (
            protected value -> string (53) "CONVERT_TZ('2016-02-29 18:00:00', '+00:00', '-06:00')"
        )
    ]
    'time' => float 0.41
]

什么可能阻止Eloquent实施正常工作?

2 个答案:

答案 0 :(得分:0)

在您的查询中

select count(*) as aggregate from `order_items` where between CONVERT_TZ('2016-02-10 09:00:00', '+00:00', '-06:00') and CONVERT_TZ('2016-02-10 18:00:00', '+00:00', '-06:00'); 

我看到你使用$ date两次。因此,Laravel生成的SQL命令如下所示:

radius1

您将在查询中获得与复制sql命令时提到的日期不同的日期。

我没有看到任何不起作用

答案 1 :(得分:0)

试试这个

whereRaw("`created_at` between
CONVERT_TZ('2016-02-17 09:00:00', '+00:00', '-06:00') and
CONVERT_TZ('2016-02-29 18:00:00', '+00:00', '-06:00')")