MySQL query statement: display data for the last 4 weeks
P粉818561682
P粉818561682 2024-04-01 13:12:56
0
1
345

How to write a query in MySQL to retrieve data for the past 4 weeks starting from today. I googled the query but I only get last week's data. The week should start today and last 4 weeks. The following is my data

| sales     |   dated           |
---------------------------------
|  12   |   01-08-2022 21:00    |
|  34   |   30-07-2022 21:00    |
|  45   |   29-07-2022 21:00    |
|  67   |   28-07-2022 21:00    |
|  89   |   27-07-2022 21:00    |
|  12   |   26-07-2022 21:00    |
|  34   |   25-07-2022 06:00    |
|  12   |   24-07-2022 21:00    |
|  23   |   23-07-2022 21:00    |
|  11   |   22-07-2022 21:00    |
|  32   |   21-07-2022 21:00    |
|  54   |   20-07-2022 21:00    |
|  65   |   19-07-2022 21:00    |
|  76   |   18-07-2022 15:00    |
|  87   |   17-07-2022 21:00    |
|  98   |   16-07-2022 21:00    |
|  18   |   15-07-2022 21:00    |
|  26   |   14-07-2022 21:00    |
|  25   |   13-07-2022 21:00    |
|  37   |   12-07-2022 21:00    |
|  38   |   11-07-2022 21:00    |
|  36   |   10-07-2022 21:00    |
|  39   |   09-07-2022 21:00    |
|  57   |   08-07-2022 21:00    |
|  45   |   06-07-2022 21:00    |
|  42   |   05-07-2022 21:00    |
|  31   |   04-07-2022 21:00    |
|  33   |   03-07-2022 21:00    |
|  22   |   02-07-2022 21:00    |
|  21   |   01-07-2022 21:00    |

I want output like below

Week-1
| sales     |   dated           |
---------------------------------
|  12   |   01-08-2022 21:00    |
|  34   |   30-07-2022 21:00    |
|  45   |   29-07-2022 21:00    |
|  67   |   28-07-2022 21:00    |
|  89   |   27-07-2022 21:00    |
|  12   |   26-07-2022 21:00    |
|  34   |   25-07-2022 06:00    |


Week-2
| sales     |   dated           |
---------------------------------
|  12   |   24-07-2022 21:00    |
|  23   |   23-07-2022 21:00    |
|  11   |   22-07-2022 21:00    |
|  32   |   21-07-2022 21:00    |
|  54   |   20-07-2022 21:00    |
|  65   |   19-07-2022 21:00    |
|  76   |   18-07-2022 15:00    |

Week-4
| sales     |   dated           |
---------------------------------
|  87   |   17-07-2022 21:00    |
|  98   |   16-07-2022 21:00    |
|  18   |   15-07-2022 21:00    |
|  26   |   14-07-2022 21:00    |
|  25   |   13-07-2022 21:00    |
|  37   |   12-07-2022 21:00    |
|  38   |   11-07-2022 21:00    |


Week-3
| sales     |   dated           |
---------------------------------
|  36   |   10-07-2022 21:00    |
|  39   |   09-07-2022 21:00    |
|  57   |   08-07-2022 21:00    |
|  45   |   06-07-2022 21:00    |
|  42   |   05-07-2022 21:00    |
|  31   |   04-07-2022 21:00    |
|  33   |   03-07-2022 21:00    |

Please accept my sincere thanks for your precious time

P粉818561682
P粉818561682

reply all(1)
P粉197639753

You can use the week number as the field value for each row:

SELECT *, FLOOR(DATEDIFF(CURRENT_DATE(), dated)/7) + 1 AS week_no 
FROM sales
WHERE dated > CURRENT_DATE() - INTERVAL 4 WEEK
ORDER BY dated DESC;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!