合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
date:today,yesterday,-2 hours,-2 day,-2 week,-2 month,-2 year 今天,昨天,2小时内,2天内,2周内,2月内,2年内 date_type:%k,%j,%e,%w,%u,%c 小时(0……23),天(001……366),天(0.....31),天(0=星期日, 6=星期六),周(0……52),月(1……12) ![](https://img.kancloud.cn/4f/b3/4fb388d8856d2605f0e008321430e2ba_437x415.png) ``` $date_type=$param['date_type']; $data[1]=db('orders')->where(['shop_id'=>$param['shop_id']])->whereTime('create_time',$param['date']) ->field("sum(money_sum) as total,DATE_FORMAT(create_time,'$date_type') as time")->group("time")->select(); $data[2]=db('orders_unpay')->where(['shop_id'=>$param['shop_id']])->whereTime('create_time',$param['date']) ->field("sum(money_sum) as total,DATE_FORMAT(create_time,'$date_type') as time")->group("time")->select(); $data[3]=db('complaint')->alias('a')->leftJoin('orders b','a.order_id=b.order_id')->where(['a.shop_id'=>$param['shop_id']]) ->whereTime('a.create_time',$param['date']) ->field("sum(b.money_sum) as total,DATE_FORMAT(a.create_time,'$date_type') as time")->group("time")->select(); return show(0,'ok',$data); ``` 参考资料 ``` 按年汇总,统计: select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y'); 按月汇总,统计:  select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y-%m'); 按季度汇总,统计:  select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3));  select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3)); 按小时:  select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by date_format(col, '%Y-%m-%d %H '); 查询 本年度的数据: SELECT * FROM mytable WHERE year(FROM_UNIXTIME(my_time)) = year(curdate()) 查询数据附带季度数: SELECT id, quarter(FROM_UNIXTIME(my_time)) FROM mytable; 查询 本季度的数据: SELECT * FROM mytable WHERE quarter(FROM_UNIXTIME(my_time)) = quarter(curdate()); 本月统计: select * from mytable where month(my_time1) = month(curdate()) and year(my_time2) = year(curdate()) 本周统计: select * from mytable where month(my_time1) = month(curdate()) and week(my_time2) = week(curdate()) N天内记录: WHERE TO_DAYS(NOW())-TO_DAYS(时间字段)<=N ``` mysql函数地址 https://www.runoob.com/mysql/mysql-functions.html