Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kundefine's avatar

Generate a last 7 days weekly report in Mysql

I want to get a report of my eco_orders table that how much order i get in a day for last 7days. i got the result with below sql and it's absolutely fine. but the problem is i want to include the non order day count with zero.

SELECT DAYNAME(created_at) AS DAY,  count(*) 
FROM `eco_orders` 
WHERE created_at >= DATE(NOW()) - INTERVAL 7 DAY 
GROUP BY DAY;

+-----------+----------+
| DAY       | count(*) |
+-----------+----------+
| Thursday  |        1 |
| Wednesday |        2 |
+-----------+----------+

expected output
+-----------+----------+
| DAY       | count(*) |
+-----------+----------+
| Saturday  |        0 |
| Sunday    |        0 |
| Monday    |        0 |
| Tuesday   |        0 |
| Wednesday |        2 |
| Thursday  |        1 |
| Friday    |        0 |
+-----------+----------+
0 likes
4 replies
Neeraj1005's avatar

@kundefine @madyson you can use carbon subDays method in laravel.

$users = YourModel::where( 'created_at', '>', Carbon::now()->subDays(7))
           ->get();

give it try as well

1 like

Please or to participate in this conversation.