如何获得拉拉韦尔的平均时间

时间:2019-02-23 05:59:11

标签: php mysql database laravel

我想获取开始时间和结束时间之间的平均时间,以及我在laravel中编写sql的CUR-Time GroupBY用户名的位置,它显示了一些错误,我找不到什么那是因为我是该laravel的新手,请帮助修复此SQL错误,然后提交我的SQL和错误消息。

 $avagTime = DB::table( 'active_user' )
            ->select(DB::raw('AVG(TIME_TO_SEC(acu_et) - TIME_TO_SEC(acu_at))'))
            ->where (DATE('acu_at') == ('CURDATE()'))
            ->get();[![enter image description here][1]][1]

enter image description here

enter image description here enter image description here

3 个答案:

答案 0 :(得分:0)

尝试此代码:

$currentTime = Carbon::now()->toDateTimeString();

$avagTime = DB::table( 'active_user' )
          ->select(DB::raw('AVG(TIME_TO_SEC(acu_et) - TIME_TO_SEC(acu_at))'))
          ->where ('acu_at', $currentTime)
          ->get();

我认为这将是有益的。谢谢

答案 1 :(得分:0)

把逻辑从那里拿出来不是更好吗?我更喜欢仅从数据库中获取日期时间,然后使用Carbon将其转换为Unix timestamp。然后使用它们,因为它们是简单的int。

答案 2 :(得分:0)

问题出在您的where子句中。 Laravel的查询生成器有一个whereDate()方法,很适合此操作:

$avagTime = DB::table('active_user')
    ->selectRaw('AVG(TIME_TO_SEC(acu_et) - TIME_TO_SEC(acu_at))')
    ->whereDate('acu_at', today())
    ->get();

注意事项:如果要像示例中那样将where原因作为原始查询传递,则需要使用whereRaw()where()之类的内容。< / p>

相关问题