如何将 TIME() 函数筛选的 mysql 查询转换为 laravel 查询生成器?
How to convert mysql query filtered by TIME() function to laravel query builder?
到目前为止,这是 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
将保存所有查询数据,return 它和 $hora1
获取输入时间并将其值传递给查询构建器 where 子句。它根本不起作用。 fecha_hora是datetime类型,有日期和时间。
如何实现?
为什么使用 whereRaw ?您可以使用 whereTime 而不是 whereRaw。这是更好的方法...例如:
->whereTime('detalle_evaluaciones.fecha_hora)', '=', $hora1);
到目前为止,这是 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
将保存所有查询数据,return 它和 $hora1
获取输入时间并将其值传递给查询构建器 where 子句。它根本不起作用。 fecha_hora是datetime类型,有日期和时间。
如何实现?
为什么使用 whereRaw ?您可以使用 whereTime 而不是 whereRaw。这是更好的方法...例如:
->whereTime('detalle_evaluaciones.fecha_hora)', '=', $hora1);