
- 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 compare timestamps in MySQL?
To compare timestamps in MySQL, you can use DATE(). Let us first create a table−
mysql> create table comparingTimestampDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> AdmissionDate timestamp -> ); Query OK, 0 rows affected (0.54 sec)
Following is the query to insert records in the table using insert command −
mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-03-31'); Query OK, 1 row affected (0.13 sec) mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-04-10'); Query OK, 1 row affected (0.12 sec) mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-04-15'); Query OK, 1 row affected (0.17 sec) mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-03-29'); Query OK, 1 row affected (0.15 sec) mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-04-07'); Query OK, 1 row affected (0.18 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from comparingTimestampDemo;
This will produce the following output −
+----+---------------------+ | Id | AdmissionDate | +----+---------------------+ | 1 | 2019-03-31 00:00:00 | | 2 | 2019-04-10 00:00:00 | | 3 | 2019-04-15 00:00:00 | | 4 | 2019-03-29 00:00:00 | | 5 | 2019-04-07 00:00:00 | +----+---------------------+ 5 rows in set (0.00 sec)
Let us now compare timestamps in MySQL −
mysql> SELECT DATE(`AdmissionDate`) FROM comparingTimestampDemo WHERE DATE(`AdmissionDate`) < CURDATE() - INTERVAL 3 DAY;
This will produce the following output −
+-----------------------+ | DATE(`AdmissionDate`) | +-----------------------+ | 2019-03-31 | | 2019-03-29 | +-----------------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to apply NOW() to timestamps field in MySQL Workbench?
- Compare specific Timestamps for a Pandas DataFrame – Python
- What is the difference between UNIX TIMESTAMPS and MySQL TIMESTAMPS?
- MySQL difference between two timestamps in Seconds?
- Difference between two timestamps in seconds in MySQL?
- Get the difference between two timestamps in seconds in MySQL?
- How to select most recent date out of a set of several possible timestamps in MySQL?
- Find the difference between two timestamps in days with MySQL
- How to compare two strings which are numbers in MySQL?
- How to get time difference between two timestamps in seconds?
- How to get current timestamp and relative timestamps in PostgreSQL?
- How can I use MySQL IN() function to compare row constructors?
- Compare date strings in MySQL
- How to get the duration between two Instant timestamps in Java
- How can we compare data in two MySQL tables?

Advertisements