有勇气的牛排博客

MySQL 根据日期查询并统计数据

有勇气的牛排 379 数据库 2023-05-18 21:54:45

文章目录

1 查询前7天的日期

select a.click_date from ( SELECT date_sub(curdate(), interval 1 day) as click_date union all SELECT date_sub(curdate(), interval 2 day) as click_date union all SELECT date_sub(curdate(), interval 3 day) as click_date union all SELECT date_sub(curdate(), interval 4 day) as click_date union all SELECT date_sub(curdate(), interval 5 day) as click_date union all SELECT date_sub(curdate(), interval 6 day) as click_date union all SELECT date_sub(curdate(), interval 7 day) as click_date ) a

查询结果

image.png

2 查询订单表出现日期,成交额总和

没有出现的日期,不会被查出来

select DATE_FORMAT(o_id,"%Y-%m-%d") as date,sum(o_turnover)*0.15 u_commission from orders where o_id between "2021-05-26" and "2021-06-02" and uid = 2 group by date;

image.png

3 根据日期统计数据

select DATE_FORMAT(date,"%Y-%m-%d") day,count(*) num from log group by day;

image.png

参考:
https://blog.csdn.net/weixin_38414968/article/details/108164479


留言

专栏
文章
加入群聊