简体   繁体   中英

How to use case in laravel raw query

I want to select the row based on the current time in laravel. I have a query but it give a empty result.

$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 "));

Try to separate fields in select and name the secound aggregated field (in my example is 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();

I also think that you have to drop the D. namespace from the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM