
- 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 the first date and the last date from a MySQL column with date records?
To compare the first date with the last date, use TIME_TO_SEC() along with MAX() and MIN(). Let us first create a table −
mysql> create table DemoTable ( UserName varchar(100), UserPostDatetime datetime ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Adam','2019-08-24 11:10:00'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Adam','2019-08-24 11:20:00'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Adam','2019-08-24 11:50:00'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------+---------------------+ | UserName | UserPostDatetime | +----------+---------------------+ | Adam | 2019-08-24 11:10:00 | | Adam | 2019-08-24 11:20:00 | | Adam | 2019-08-24 11:50:00 | +----------+---------------------+ 3 rows in set (0.00 sec)
Following is the query to compare the first date and the last date −
mysql> select UserName,sec_to_time(max(time_to_sec(UserPostDatetime))-min(time_to_sec(UserPostDatetime)))t from DemoTable group by UserName;
This will produce the following output −
+----------+----------+ | UserName | t | +----------+----------+ | Adam | 00:40:00 | +----------+----------+ 1 row in set (0.03 sec)
- Related Articles
- How to find last date from records with date values in MySQL?
- MySQL query to fetch the latest date from a table with date records
- How to display first day and last day of the month from date records in MySQL?
- How to change the date in a table with date records with MySQL?
- Display month names and year from a column with date records with MySQL
- Display distinct dates in MySQL from a column with date records
- How to subtract seconds from all the date records in a MySQL column?
- Find the difference between current date and the date records from a MySQL table
- Fastest way to search for a date from the date records in MySQL
- Comparison of varchar date records from the current date in MySQL
- Get Nth weekday of the month from a column with DATE records in MySQL
- How to get the difference between date records and the current date in MySQL?
- How to update column values with date records and set 1 for corresponding records before the current date in SQL
- Display record with today and tomorrow’s date from a column with date record in MySQL
- MySQL date column auto fill with current date?

Advertisements