如何在laravel原始查询中使用案例

时间:2016-05-02 11:16:33

标签: php laravel laravel-4 laravel-5

我想根据laravel中的当前时间选择行。我有一个查询,但它给出一个空的结果。

$price_calc = DB::table('trip_price_fixing')
            ->select(DB::raw("DATE(D.datumTijd),
     CASE WHEN TIME(D.datumTijd) BETWEEN '08:00:00' AND '14:00:00' THEN 1
          WHEN TIME(D.datumTijd) BETWEEN '14:00:00' AND '20:00:00' THEN 2
          WHEN TIME(D.datumTijd) BETWEEN '20:00:00' AND '24:00:00' THEN 3
     END "));

1 个答案:

答案 0 :(得分:0)

尝试在select中分隔字段并命名secound聚合字段(在我的示例中为result):

$price_calc = DB::table('trip_price_fixing')
        ->select(DB::raw("DATE(D.datumTijd) as date",
                 DB::raw("(CASE WHEN TIME(D.datumTijd) BETWEEN '08:00:00' AND '14:00:00' THEN 1
                          WHEN TIME(D.datumTijd) BETWEEN '14:00:00' AND '20:00:00' THEN 2
                          WHEN TIME(D.datumTijd) BETWEEN '20:00:00' AND '24:00:00' THEN 3
 END) as result"))->get();

我还认为你必须从查询中删除D.命名空间。