zoukankan      html  css  js  c++  java
  • mysql查询两个日期之间相差多少天?

    需求描述:

      在mysql中,查看两个日期之间相差多少天

    操作过程:

    1.通过datediff函数,查看两个日期之间相差多少天

    mysql> select datediff('2018-06-26','2018-06-25'),datediff('2018-06-20','2018-06-26');
    +-------------------------------------+-------------------------------------+
    | datediff('2018-06-26','2018-06-25') | datediff('2018-06-20','2018-06-26') |
    +-------------------------------------+-------------------------------------+
    |                                   1 |                                  -6 |
    +-------------------------------------+-------------------------------------+
    1 row in set (0.00 sec)

    备注:datediff(expr1,expr2),一般返回的就是expr1-expr2的差值,结果可以是正数,也可以是负数.主要是两个日期之间相差多少天.

    2.带有时分秒的表达式

    mysql> select datediff('2018-06-26 22:00:00','2018-06-25'),datediff('2018-06-20','2018-06-26 21:00:00');
    +----------------------------------------------+----------------------------------------------+
    | datediff('2018-06-26 22:00:00','2018-06-25') | datediff('2018-06-20','2018-06-26 21:00:00') |
    +----------------------------------------------+----------------------------------------------+
    |                                            1 |                                           -6 |
    +----------------------------------------------+----------------------------------------------+
    1 row in set (0.00 sec)

    备注:在日期计算中,如果存在时分秒的部分,是会被忽略的只对日期的部分进行计算即只对天计算.

    文档创建时间:2018年6月26日12:47:32

    官方文档参考:

    DATEDIFF(expr1,expr2)
    
    DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions.
    Only the date parts of the values are used in the calculation. mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30'); -> 1 mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31'); -> -31
  • 相关阅读:
    【JAVASCRIPT】call和apply的用法以及区别
    web开发中的支付宝支付和微信支付
    【input】标签去除默认样式
    npm run build后如何打开index.html跑起项目
    Sass的混合-@mixin,@include
    ios h5 长按时出现黑色透明遮罩
    ios h5 长按放大镜效果关闭
    vue.$nextTick 解决了哪些问题
    原生JS代码封装(将字符串转换为日期 2019.08.24 )
    原生JS代码封装(获取年月日时分秒 )
  • 原文地址:https://www.cnblogs.com/chuanzhang053/p/9228569.html
Copyright © 2011-2022 走看看