
- 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
Delete records where timestamp older than 5 minutes in MySQL?
For this, use DELETE command. Let us first create a table −
mysql> create table DemoTable1851 ( DueDate datetime ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1851 values('2019-12-03 21:30:35'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1851 values('2019-12-03 21:45:00'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1851 values('2019-12-03 21:34:00'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1851;
This will produce the following output −
+---------------------+ | DueDate | +---------------------+ | 2019-12-03 21:30:35 | | 2019-12-03 21:45:00 | | 2019-12-03 21:34:00 | +---------------------+ 3 rows in set (0.00 sec)
The current date time is as follows −
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-12-03 21:38:46 | +---------------------+ 1 row in set (0.00 sec)
Here is the query to delete records where timestamp is older than 5 minutes −
mysql> delete from DemoTable1851 where DueDate < (NOW() - INTERVAL 5 MINUTE); Query OK, 2 rows affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1851;
This will produce the following output −
+---------------------+ | DueDate | +---------------------+ | 2019-12-03 21:45:00 | +---------------------+ 1 row in set (0.00 sec)
- Related Articles
- Fetching rows updated at timestamp older than 1 day in MySQL?
- How to delete rows older than 14 days in MySQL?
- MySQL query to delete all rows older than 30 days?
- Deleting all rows older than 5 days in MySQL
- How to select all records that are 10 minutes within current timestamp in MySQL?
- MySQL query to return all records with a datetime older than 1 week
- MySQL query to delete a DATE older than 30 days from another date?
- Delete selective multiple records using MySQL DELETE query
- Delete all records from a table in MySQL?
- Order records and delete n rows in MySQL
- Write a MySQL query where length of the records is longer than 1?
- MySQL Select where timestamp is in the current hour?
- Selecting rows that are older than current date in MySQL?
- Pull MySQL records for the last 60 minutes?
- MySQL query to select rows older than a week?

Advertisements