如何将由TIME()函数过滤的mysql查询转换为laravel查询生成器?

问题描述 投票:1回答:1

到目前为止,这是MySQL查询,它完美地运行。它通过将传递的值与TIME(fecha_hora)匹配来过滤记录:

 select nombre_departamento, categoria_respuesta, count(valor_evaluacion) from 
       detalle_evaluaciones JOIN departamentos ON departamentos.id = 
       detalle_evaluaciones.departamentos_id JOIN tipo_respuestas ON tipo_respuestas.id = detalle_evaluaciones.tipo_respuestas_id 
        where TIME(fecha_hora) = '06:13:00'

    group by nombre_departamento, categoria_respuesta ORDER BY `tipo_respuestas`.`id` desc 

这是我尝试过的:

$hora = $request->get('hora');
  $hora1 = date('H:i:s', strtotime("$hora"));
        $calif = DB::table ('detalle_evaluaciones')->select (DB::raw('departamentos.nombre_departamento,tipo_respuestas.categoria_respuesta,   detalle_evaluaciones.valor_evaluacion, COUNT(detalle_evaluaciones.valor_evaluacion) as cantidad'))
      ->join  ('departamentos','detalle_evaluaciones.departamentos_id','=','departamentos.id')
      ->join  ('tipo_respuestas','detalle_evaluaciones.tipo_respuestas_id','=','tipo_respuestas.id')
      ->whereRaw('(TIME(detalle_evaluaciones.fecha_hora)=?)',$hora1)

      ->groupby ('departamentos.nombre_departamento','tipo_respuestas.categoria_respuesta')

      ->orderby ('tipo_respuestas.id','DESC')
      ->paginate(10);

$calif将保存所有查询数据并将其返回,$hora1正在获取输入时间并将其值传递给query builder where子句。它根本不起作用。 fecha_hora是datetime类型,它有日期和时间。

怎么做到这一点?

mysql laravel-5.5 laravel-query-builder
1个回答
0
投票

你为什么用whereRaw?您可以使用whereTime而不是whereRaw。这是更好的方式...例如:

->whereTime('detalle_evaluaciones.fecha_hora)', '=', $hora1);
© www.soinside.com 2019 - 2024. All rights reserved.