
- 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
MySQL query to check how to get time difference
Let us first create a table −
mysql> create table DemoTable1570 -> ( -> ArrivalTime datetime -> ); Query OK, 0 rows affected (0.87 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1570 values('2019-10-15 5:10:00'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1570 values('2019-10-15 23:00:00'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1570 values('2019-10-15 23:10:00'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1570 values('2019-10-15 16:10:00'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1570;
This will produce the following output
+---------------------+ | ArrivalTime | +---------------------+ | 2019-10-15 05:10:00 | | 2019-10-15 23:00:00 | | 2019-10-15 23:10:00 | | 2019-10-15 16:10:00 | +---------------------+ 4 rows in set (0.00 sec)
The current date is as follows −
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-10-15 22:26:14 | +---------------------+ 1 row in set (0.00 sec)
Following is the query to get time difference −
mysql> select * from DemoTable1570 -> where ArrivalTime > now() - interval 5 hour;
This will produce the following output
+---------------------+ | ArrivalTime | +---------------------+ | 2019-10-15 23:00:00 | | 2019-10-15 23:10:00 | +---------------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to measure actual MySQL query time?
- Get the time difference and convert it to hours in MySQL?
- How to get the Average on MySQL time column?
- How To Check If The Time Difference Is Greater Than A Specific Time?
- How to get the fourth highest value using MySQL query?
- How to get multiple rows in a single MySQL query?
- How to get time difference between two timestamps in seconds?
- MySQL query to check if multiple rows exist?
- MySQL query to insert current date plus specific time?
- MySQL Query to get count of unique values?
- How can I check how much time MySQL query, without printing it on the console, is taking?
- How to multiple insert or batch insert at a time in MySQL query?
- MySQL query to extract time in a format without seconds
- MySQL query to select rows one batch at a time
- How to check the difference between plot generation time in base R?

Advertisements