
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- Fetch datetime row from exactly past 7 days records in MySQL
- MySQL time period query to fetch date records from interval of 14 weeks from current date?
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- Fetch student records whose result declared 12 days before the current date in MYSQL
- Comparison of varchar date records from the current date in MySQL
- Select a column if condition is met in MySQL to fetch records from current date and current date + 1
- How to get the records of the last two days from the current date in MySQL?
- Calling NOW() function to fetch current date records in MySQL?
- MySQL query to fetch the latest date from a table with date records
- Fetch date records comparing with the current date’s day and month in MySQL
- Find the difference between current date and the date records from a MySQL table
- Add records from corresponding duplicate values in another column with MySQL
- Add 30 days to date in a MySQL table with arrival date records
- Display records from the current date till rest of the same month in MySQL?
- How to ignore specific records and add remaining corresponding records (numbers) in MySQL?

Advertisements