![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
Insert current time minus 1 hour to already inserted date-time records in MYSQL
For subtracting dates, use MySQL DATE_SUB(). Let us first create a −
mysql> create table DemoTable1434 -> ( -> ArrivalDatetime datetime -> ); Query OK, 0 rows affected (3.14 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1434 values('2019-09-30 21:10:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1434 values('2018-09-30 22:20:40'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable1434 values('2017-09-30 23:10:00'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select −
mysql> select * from DemoTable1434;
This will produce the following output −
+---------------------+ | ArrivalDatetime | +---------------------+ | 2019-09-30 21:10:00 | | 2018-09-30 22:20:40 | | 2017-09-30 23:10:00 | +---------------------+ 3 rows in set (0.00 sec)
Following is the query to insert current time minus 1 hour. We will update here −
mysql> update DemoTable1434 -> set ArrivalDatetime=date_sub(now(),interval 1 hour); Query OK, 3 rows affected (0.19 sec) Rows matched: 3 Changed: 3 Warnings: 0
The current date time is as follows −
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-09-30 21:16:48 | +---------------------+ 1 row in set (0.00 sec)
Let us check the table records once again −
mysql> select * from DemoTable1434;
This will produce the following output −
+---------------------+ | ArrivalDatetime | +---------------------+ | 2019-09-30 20:16:39 | | 2019-09-30 20:16:39 | | 2019-09-30 20:16:39 | +---------------------+ 3 rows in set (0.00 sec)
Advertisements