zoukankan      html  css  js  c++  java
  • Laravel 查询数据按照时间分组

    首先取消严格模式:

    // config/database.php
    // 'strict' => true, // 严谨模式注释掉

    查询构造器代码:

    //查询构造器部分代码
    })->with(['device_field', 'device_field.device_field_log' => function($query) use($request){
                // 时间
                $time = floatval($request->input('time'));
                if($time > 8760){
                    $time = 8760;
                }else if($time < 0.5){
                    $time = 0.5;
                }
                // 查询时间区间 
                // $query->whereBetween('created_at',[date("Y-m-d H:i:s", time() - ($time * 3600)),date("Y-m-d H:i:s", time())])->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') as date, id,device_id,device_field_id,name,created_at")->groupBy('date')->orderBy('date', 'desc');
                $query->whereBetween('created_at',[date("Y-m-d H:i:s", time() - ($time * 3600)),date("Y-m-d H:i:s", time())]);
                $query->select("*"); // 设置要查询的字段(如果设置必须包含关联的字段,否则数据查询不到,如本例(device_id,device_field_id) 如:$query->select("device_id,device_field_id")
                $query->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') as date, id,device_id,device_field_id,name,created_at");
                $query->groupBy('date');
                $query->orderBy('date', 'desc');
            }])->get();

     参考链接:

    1. https://www.jianshu.com/p/a2a2c36361d9 
    2. https://www.jianshu.com/p/0d57562e1c04
  • 相关阅读:
    print输出带颜色特效的方法
    为什么pymysql执行SQL语句后提示成功,但没有实际操作数据库?
    Mysql支持的数据类型
    基本的SQL语句
    初次运行Git前的配置
    git安装
    Hadoop HA 搭建
    Ubuntu下编译 Hadoop2.9
    Mac 下编译 Hadoop
    Windows下编译 Hadoop
  • 原文地址:https://www.cnblogs.com/xiaqiuchu/p/11532924.html
Copyright © 2011-2022 走看看