![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetch records from interval of past 3 days from current date in MySQL and add the corresponding records
Let us first create a table −
mysql> create table DemoTable ( ProductAmount int, PurchaseDate datetime ); Query OK, 0 rows affected (0.94 sec)
Note − Let’s say the current date is 2010-09-15.
Insert some records in the table using insert command −
mysql> insert into DemoTable values(567,'2019-09-10'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(1347,'2019-09-14'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(2033,'2019-09-13'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(1256,'2019-09-11'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(1000,'2019-09-16'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------+---------------------+ | ProductAmount | PurchaseDate | +---------------+---------------------+ | 567 | 2019-09-10 00 :00 :00 | | 1347 | 2019-09-14 00:00 :00 | | 2033 | 2019-09-13 00:00 :00 | | 1256 | 2019-09-11 00:00 :00 | | 1000 | 2019-09-16 00:00 :00 | +---------------+---------------------+ 5 rows in set (0.00 sec)
Following is the query to use now() function in MySQL query −
mysql> select sum(ProductAmount) from DemoTable where PurchaseDate > NOW()- interval 3 day;
This will produce the following output −
+--------------------+ | sum(ProductAmount) | +--------------------+ | 4380 | +--------------------+ 1 row in set (0.00 sec)
Advertisements