
- 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
MySQL DATE_ADD() to increment a date based on the value in another column?
Let us first create a table with one of the columns as DueDate and another one “RepeatTime, which displays how many times, let’s say a user was reminded to submit the payment −
mysql> create table DemoTable -> ( -> DueDate date, -> RepeatTime int -> ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-01-23',3); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-06-22',6); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-03-28',2); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('2017-07-25',6); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+------------+------------+ | DueDate | RepeatTime | +------------+------------+ | 2019-01-23 | 3 | | 2019-06-22 | 6 | | 2019-03-28 | 2 | | 2017-07-25 | 6 | +------------+------------+ 4 rows in set (0.00 sec)
Here is the query that increments date based in the values in another column −
mysql> update DemoTable set DueDate=date_add(DueDate,interval RepeatTime MONTH) where RepeatTime!=6; Query OK, 2 rows affected (0.25 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check table records once again −
mysql> select *from DemoTable;
Output
This will produce the following output −
+------------+------------+ | DueDate | RepeatTime | +------------+------------+ | 2019-04-23 | 3 | | 2019-06-22 | 6 | | 2019-05-28 | 2 | | 2017-07-25 | 6 | +------------+------------+ 4 rows in set (0.00 sec)
- Related Articles
- Perform MySQL UPDATE on the basis of DATE value in another column
- Increment date/time value by second with MySQL query?
- Update a column based on another MySQL table’s column
- Increment column value ‘ADD’ with MySQL SET clause
- Exclude rows based on column value when another duplicate column value is found in MySQL?
- Add a column to a MySQL table which is the result of concatenation of text and value from another auto increment column?
- Get first date from timestamp in MySQL group by another column with duplicate value
- How to calculate a future date based on a given date in Excel?
- Add a single year to all the date records in a MySQL table column
- Calculate age based on date of birth in MySQL?
- MySQL add days to a date?
- How to calculate quarter start date or end date based on a given date in Excel?
- How to insert DATE into a MySQL column value using Java?
- How to assign a column value in a data frame based on another column in another R data frame?
- Add a temporary column in MySQL where the values depend on another column?

Advertisements