zoukankan      html  css  js  c++  java
  • [Linux运维 -- 数据库]mysql处理时间

    [Linux运维 -- 数据库]mysql处理时间

    基本用法:

    (1)获取时间日期

    • 基本时间
    mysql> select now();
    +---------------------+
    | now()               |
    +---------------------+
    | 2015-08-24 14:48:01 |
    +---------------------+
    1 row in set (0.00 sec)
    
    mysql> select curdate();
    +------------+
    | curdate()  |
    +------------+
    | 2015-08-24 |
    +------------+
    
    • 获取星期几
    mysql> select dayofweek(now());
    +------------------+
    | dayofweek(now()) |
    +------------------+
    |                2 |
    +------------------+
    
    mysql> select dayofweek(curdate());
    +----------------------+
    | dayofweek(curdate()) |
    +----------------------+
    |                    2 |
    +----------------------+
    
    • 获取这个月的第几天
    mysql> select dayofmonth(now());
    +-------------------+
    | dayofmonth(now()) |
    +-------------------+
    |                24 |
    +-------------------+
    1 row in set (0.00 sec)
    
    • 获取这一年的第几天
    mysql> select dayofyear(now());
    +------------------+
    | dayofyear(now()) |
    +------------------+
    |              236 |
    +------------------+
    1 row in set (0.00 sec)
    

    (2)动态获取时间

    • 获取一天前
    mysql> select date_sub(now(), interval 1 day);
    +---------------------------------+
    | date_sub(now(), interval 1 day) |
    +---------------------------------+
    | 2015-08-23 20:06:40             |
    +---------------------------------+
    
    mysql> select date_add(now(), interval -1 day);
    +----------------------------------+
    | date_add(now(), interval -1 day) |
    +----------------------------------+
    | 2015-08-23 20:09:20              |
    +----------------------------------+
    
    • 获取一个小时前
    mysql> select date_add(now(), interval -1 hour);
    mysql> select date_sub(now(), interval 1 hour);
    
    • 获取一分钟前
    mysql> select date_add(now(), interval -1 minute);
    mysql> select date_sub(now(), interval 1 minute);
    

    (3) DATE_FORMAT

    >select DATE_FORMAT(now(), '%Y-%m-%d %H:%i:%s');
    +-----------------------------------------+
    | DATE_FORMAT(now(), '%Y-%m-%d %H:%i:%s') |
    +-----------------------------------------+
    | 2016-01-21 11:28:54                     |
    +-----------------------------------------+
    

    链接: http://www.w3school.com.cn/sql/func_date_format.asp

    参考:

    (1) http://www.cnblogs.com/zeroone/archive/2010/05/05/1727659.html
    (2) http://justdo2008.iteye.com/blog/1141609

  • 相关阅读:
    java往文本文件中写入信息并修改
    idea中写servlet时报错--关于405错误
    关于引入js文件乱码的问题
    关于HTML的引入CSS文件问题
    后缀算法
    vim中 E212:无法打开并写入文件的解决办法
    Restful风格API
    Restful概念
    Andriod实现推送的解决方案(转)
    IOS系统推送原理
  • 原文地址:https://www.cnblogs.com/zk47/p/4755782.html
Copyright © 2011-2022 走看看