gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:39:34 25 4
gpt4 key购买 nike

到目前为止,这是 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 获取输入时间并将其值传递给查询构建器 where 子句。它根本不起作用。 fecha_hora 是datetime类型,有日期和时间。

如何实现?

最佳答案

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

->whereTime('detalle_evaluaciones.fecha_hora)', '=', $hora1);

关于mysql - 如何将 TIME() 函数过滤的 mysql 查询转换为 laravel 查询生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52346021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com