search
HomeDatabaseMysql TutorialHow to find the time difference in mysql
How to find the time difference in mysqlDec 27, 2021 pm 06:27 PM
mysqlTime difference

Mysql method to find the time difference: 1. Use the TIMEDIFF() function, the syntax "TIMEDIFF(dt1, dt2);"; 2. Use the IMESTAMPDIFF() function, the syntax "TIMESTAMPDIFF(unit,begin,end); ".

How to find the time difference in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

mysql finds the time difference

  • timediff() function - calculates the difference between two TIME or DATETIME values .

  • timestampdiff() function - Calculates the difference between two DATE or DATETIME values.

MySQL TIMEDIFF function introduction

TIMEDIFFReturns between two TIME or DATETIME values difference. See the following syntax for the TIMEDIFF function.

TIMEDIFF(dt1, dt2);

TIMEDIFF The function accepts two parameters that must be of the same type, namely TIME or DATETIME. The TIMEDIFF function returns the result of dt1 - dt2 expressed as a time value.

Because the TIMEDIFF function returns a TIME value, its result is limited to the range from -838:59:59 to 838: 59:59 is within the value range of TIME.

MySQL TIMEDIFF Function Example

Let us take an example to calculate the difference between two time values.

mysql> SELECT TIMEDIFF('12:00:00','10:00:00') diff;
+----------+
| diff     |
+----------+
| 02:00:00 |
+----------+
1 row in set

In this example, we calculated the difference between 12:00:00 and 10:00:00 as: 02: 00:00.

The following example calculates the difference between two DATETIME values:

mysql> SELECT TIMEDIFF('2010-01-01 01:00:00', '2010-01-02 01:00:00') diff;
+-----------+
| diff      |
+-----------+
| -24:00:00 |
+-----------+
1 row in set

If either argument is NULL, TIMEDIFFThe function will return NULL.

mysql> SELECT TIMEDIFF('2010-01-01',NULL) diff;
+------+
| diff |
+------+
| NULL |
+------+
1 row in set, 1 warning (0.00 sec)

If you pass two parameters of different types, one is DATETIME and the other is TIME, the TIMEDIFF function also returns NULL.

mysql> SELECT TIMEDIFF('2010-01-01 10:00:00','10:00:00') diff;
+------+
| diff |
+------+
| NULL |
+------+
1 row in set

MySQL TIMESTAMPDIFF function introduction

The following explains the syntax of the TIMESTAMPDIFF function.

TIMESTAMPDIFF(unit,begin,end);

TIMESTAMPDIFF function returns the result of begin-end, where begin and end are DATE or DATETIME expression. The

TIMESTAMPDIFF function allows its arguments to have mixed types, for example, begin is a DATE value and end can be DATETIME value. If a DATE value is used, the TIMESTAMPDIFF function treats it as a DATETIME value with the time part "00:00:00". The

unit parameter is the unit used to determine the result of (end-begin), expressed as an integer. The following are valid units:

  • MICROSECOND
  • SECOND
  • MINUTE
  • HOUR
  • DAY
  • WEEK
  • MONTH
  • QUARTER
  • YEAR

##MySQL TIMESTAMPDIFF function example

The following example will return the difference between

2018-01-01 and 2018-06-01 as a month value:

mysql> SELECT TIMESTAMPDIFF(MONTH, '2018-01-01', '2018-06-01') result;
+--------+
| result |
+--------+
|      5 |
+--------+
1 row in set
If you want to see Difference, just change the

unit parameter from MONTH to DAY, as shown below:

mysql> SELECT TIMESTAMPDIFF(DAY, '2010-01-01', '2010-06-01') result;
+--------+
| result |
+--------+
|    151 |
+--------+
1 row in set
The following statement returns two

DATETIME Difference value in minutes:

mysql> SELECT TIMESTAMPDIFF(MINUTE, '2018-01-01 10:00:00', '2018-01-01 10:45:00') result;
+--------+
| result |
+--------+
|     45 |
+--------+
1 row in set
Please note that

TIMESTAMPDIFF only considers the portion of time associated with the unit parameter. See the example below:

mysql> SELECT TIMESTAMPDIFF(MINUTE, '2018-01-01 10:00:00', '2018-01-01 10:45:59') result;
+--------+
| result |
+--------+
|     45 |
+--------+
1 row in set
The difference should be 45 minutes and 59 seconds. However, we passed the

unit parameter as MINUTE, so the function returns 45 minutes as expected.

If

SECOND is used instead of MINUTE, the TIMESTAMPDIFF function will consider the SECOND part, as shown in the following example :

mysql> SELECT TIMESTAMPDIFF(SECOND, '2018-01-01 10:00:00', '2018-01-01 10:45:59') result;
+--------+
| result |
+--------+
|   2759 |
+--------+
1 row in set
Note: 45 minutes and 59 seconds = 45×60 59 (seconds) = 2759 seconds

Use the MySQL TIMESTAMPDIFF function to calculate age

First , we create a new table named persons for demonstration purposes.

USE testdb;
CREATE TABLE persons (
    id INT AUTO_INCREMENT PRIMARY KEY,
    full_name VARCHAR(255) NOT NULL,
    date_of_birth DATE NOT NULL
);

Second, insert some rows into the persons table:

INSERT INTO persons(full_name, date_of_birth)
VALUES('John Doe', '1990-01-01'),
      ('David Taylor', '1989-06-06'),
      ('Peter Drucker', '1985-03-02'),
      ('Lily Minsu', '1992-05-05'),
      ('Mary William', '1995-12-01');

The third step, use TIMESTAMPDIFF to calculate the age of each person in the persons table:

SELECT 
    id,
    full_name,
    date_of_birth,
    TIMESTAMPDIFF(YEAR,
        date_of_birth,
        '2018-01-01') age
FROM
    persons;
Execute the above query statement and get the following results-

+----+---------------+---------------+-----+
| id | full_name     | date_of_birth | age |
+----+---------------+---------------+-----+
|  1 | John Doe      | 1990-01-01    |  28 |
|  2 | David Taylor  | 1989-06-06    |  28 |
|  3 | Peter Drucker | 1985-03-02    |  32 |
|  4 | Lily Minsu    | 1992-05-05    |  25 |
|  5 | Mary William  | 1995-12-01    |  22 |
+----+---------------+---------------+-----+
5 rows in set
In this statement, we calculate Age as of

2018-01-01. If you want to calculate the current age, you can replace the literal value '2018-01-01' with the NOW function, as shown below:

SELECT 
    id,
    full_name,
    date_of_birth,
    TIMESTAMPDIFF(YEAR,
        date_of_birth,
        NOW()) age
FROM
    persons;
Execute the above query statement to get The following results-

+----+---------------+---------------+-----+
| id | full_name     | date_of_birth | age |
+----+---------------+---------------+-----+
|  1 | John Doe      | 1990-01-01    |  27 |
|  2 | David Taylor  | 1989-06-06    |  28 |
|  3 | Peter Drucker | 1985-03-02    |  32 |
|  4 | Lily Minsu    | 1992-05-05    |  25 |
|  5 | Mary William  | 1995-12-01    |  21 |
+----+---------------+---------------+-----+
5 rows in set
[Related recommendations:

mysql video tutorial]

The above is the detailed content of How to find the time difference in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!