
- 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
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 Articles
- Can MySQL automatically store timestamp in a row?
- MySQL query to insert row with date?
- Select rows from a table with date between 90 days ago and now in MySQL
- How to Insert custom date into MySQL timestamp field?
- Insert row with only default values in MySQL
- MySQL insert a value to specific row and column
- How can we insert a new row into a MySQL table?
- Get two days data (today and yesterday) from a MySQL table with timestamp values
- PHP program to convert a given timestamp into time ago
- MySQL: Insert a row and get the content?
- How to insert Timestamp value in a database using JDBC program?
- Select entries with timestamp after X time in MySQL
- How to insert an array of values in a MySQL table with a single INSERT?
- Creating a table with a TIMESTAMP field in MySQL?
- How to find specific row with a MySQL query?

Advertisements