

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to insert a row with a timestamp “X days ago” in MySQL?
To insert a row like this use the following syntax, wherein we are using CURRENT_TIMESTAMP −
insert into yourTableName values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY);
To understand the above syntax, let us create a table −
mysql> create table DemoTable1983 ( DueDate timestamp ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1983 values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1983 values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1983 values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1983;
This will produce the following output −
+---------------------+ | DueDate | +---------------------+ | 2019-12-10 19:35:43 | | 2019-10-27 19:35:44 | | 2019-11-10 19:35:45 | +---------------------+ 3 rows in set (0.00 sec)
The current date time is as follows −
mysql> select now();
This will produce the following output −
+---------------------+ | now() | +---------------------+ | 2019-12-18 19:36:17 | +---------------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- Select rows from a table with date between 90 days ago and now in MySQL
- PHP program to convert a given timestamp into time ago
- Can MySQL automatically store timestamp in a row?
- MySQL query to insert row with date?
- Select entries with timestamp after X time in MySQL
- Get two days data (today and yesterday) from a MySQL table with timestamp values
- MySQL insert a value to specific row and column
- How to Insert custom date into MySQL timestamp field?
- How can we insert a new row into a MySQL table?
- Insert row with only default values in MySQL
- MySQL: Insert a row and get the content?
- How to insert Timestamp value in a database using JDBC program?
- Creating a table with a TIMESTAMP field in MySQL?
- How to insert an array of values in a MySQL table with a single INSERT?
- MySQL query to fetch records wherein timestamp is before 15+ days?
Advertisements