phpmyadmin 如何在mysql中获取同一表中不同行本周和上周记录

o8x7eapl  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(146)

大家好,我正在写mysql查询,以获得本周和上周的数据,在同一个表中,不同的行预期输出,我在下面提到,我还需要上周的数据,根据本周的选择。本周的数据,我得到的选择日期,但不知道如何获得上周的日期,请帮助我。
| 日期|汽车总数|联机小时数|行程|
| - -|- -|- -|- -|
| 当前周|20个|100个|二百个|
| 最后一周(_W)|10个|六十个|八十个|

查询本周记录

SELECT cd.date, COUNT( cd.car_number) as total_cars, sum(cd.trips) as total_trips,
sum(cd.online_hours) as online_hours
FROM fleet_car_dash_daily as cd
WHERE cd.team_id= 1 and cd.date BETWEEN '2022-04-04' and '2022-04-10'
e5nqia27

e5nqia271#

这也有助于:

SELECT cd.date, COUNT( cd.car_number) as total_cars, sum(cd.trips) as total_trips,
sum(cd.online_hours) as online_hours
FROM fleet_car_dash_daily as cd
WHERE cd.team_id= 1 and cd.date BETWEEN '2022-04-04' - INTERVAL 7 DAY and '2022-04-10'
GROUP BY WEEK(cd.date)
ORDER BY WEEK(cd.date);

相关问题