php - Get fix number of records by grouping of date according to registered date of the user
Get the solution ↓↓↓I am stuck with a query, and not sure how can we achieve this -
I have one user table and another is user_transactions table. I need to get the data from transaction table according to date of user created. Interesting thing is need to get only 10 or less records always.
for example today is 30 Aug 2020,and User registered on 21 Aug 2020, then the result will be -
21 Aug 2020 : 1050
22 Aug 2020 : 100
...
..
30 Aug 2020 : 1000
It mean it will show the date from 21 August - 30 August with single date and total transactions on that date.
But now If the user is registered on 01 August 2020, then total 30 days are completed, but still I need 10 records and the result will be --
01 Aug 2020 - 03 Aug 2020 - 5000
04 Aug 2020 - 06 Aug 2020 - 50000
...
...
28 Aug 2020 - 30 Aug 2020 - 8000
It means total 10 records returns with total transactions of 3 - 3 days. If that from 1 year then sum of each 36 days transactions will be returns in total 10 rows.
Please help me for this query.
here is my table structure ----
**users table ---
id | name | email | created_at
transactions table ----
id | user_id | amount | created_at**
Now I need to get data based on the user_id according to that user created_at and sum of amount based on the transactions created_at date
Thanks in advance.
Answer
Solution:
While you are not answering, I will make a guess...
So here is a table:
create table transactions(
id int NOT NULL AUTO_INCREMENT,
user_id int,
amount int,
created_at timestamp,
PRIMARY KEY (id)
);
The table test data (do not run it in code snippet - this is just to make expandable box):
insert into transactions (user_id, amount, created_at) values
(1, 1050, '2020-08-15'),
(1, 150, '2020-08-15 10:00:00'),
(1, 1050, '2020-08-16'),
(1, 150, '2020-08-16 10:00:00'),
(1, 1050, '2020-08-17'),
(1, 150, '2020-08-17 10:00:00'),
(1, 1050, '2020-08-18'),
(1, 150, '2020-08-18 10:00:00'),
(1, 1050, '2020-08-19'),
(1, 150, '2020-08-19 10:00:00'),
(1, 1050, '2020-08-20'),
(1, 150, '2020-08-20 10:00:00'),
(1, 1050, '2020-08-21'),
(1, 150, '2020-08-21 10:00:00'),
(1, 500, '2020-08-22'),
(1, 200, '2020-08-22 12:00:00'),
(1, 1000, '2020-08-23'),
(1, 100, '2020-08-23 01:01:01'),
(1, 50, '2020-08-24'),
(1, 50, '2020-08-24 14:00:00'),
(1, 150, '2020-08-25'),
(1, 350, '2020-08-25 13:13:13'),
(1, 250, '2020-08-26'),
(1, 250, '2020-08-26 14:14:24'),
(1, 450, '2020-08-27'),
(1, 40, '2020-08-27 11:11:11'),
(1, 300, '2020-08-28'),
(1, 300, '2020-08-28 12:12:12'),
(1, 1150, '2020-08-29'),
(1, 115, '2020-08-29 12:12:12'),
(1, 1250, '2020-08-30')
;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.