
- 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
Increment multiple Timestamp values by setting the incremented value in a user-defined variable in SQL
The incremented value can be set in a user-defined variable as shown below. Here, “yourValue” is the incremented value. After that, use MySQL UPDATE to update the column and increment timestamp values −
set @anyVariableName :=yourValue; update yourTableName set yourColumnName=yourColumnName+interval (@yourVariableName) second;
Let us first create a table −
mysql> create table DemoTable ( DueDatetime timestamp ); Query OK, 0 rows affected (0.73 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-01-31 12 :30 :40'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('2019-09-06 10 :00 :00'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('2019-09-07 11 :10 :24'); Query OK, 1 row affected (0.25 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------------------+ | DueDatetime | +-----------------------+ | 2019-01-31 12 :30 :40 | | 2019-09-06 10 :00 :00 | | 2019-09-07 11 :10 :24 | +-----------------------+ 3 rows in set (0.00 sec)
Following is the query to increment multiple timestamp values −
mysql> set @secondValue :=12; Query OK, 0 rows affected (0.00 sec) mysql> update DemoTable set DueDatetime=DueDatetime+interval (@secondValue) second; Query OK, 3 rows affected (0.99 sec) Rows matched : 3 Changed : 3 Warnings : 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output. The timestamp values are now incremented −
+-----------------------+ | DueDatetime | +-----------------------+ | 2019-01-31 12 :30 :52 | | 2019-09-06 10 :00 :12 | | 2019-09-07 11 :10 :36 | +-----------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Get the maximum exam date using a user-defined variable in SQL
- How can we store a value in user-defined variable?
- In MySQL, why a client cannot use a user-defined variable defined by another client?
- Updating a MySQL table row column by appending a value from user defined variable?
- MySQL ORDER BY with numeric user-defined variable?
- How to set different auto-increment ids for two tables with a user-defined variable?
- Select into a user-defined variable with MySQL
- Perform MySQL SELECT INTO user-defined variable
- Show a MySQL user-defined variables values in the result table?
- Set custom messages by working with MySQL IF Statements and SELECT in a user-defined variable
- Swap two Strings without using third user defined variable in Java
- Set user-defined variable with table name in MySQL prepare statement?
- How Can we permanently define user-defined variable for a client in MySQL?
- Increase database field value by specified percentage using user-defined variables in MySQL
- Add user defined value to a column in a MySQL query?

Advertisements