zoukankan      html  css  js  c++  java
  • MySQL 按照日期格式查询带有时间戳数据

    按照日期格式查询带有时间戳数据
    一般在MSQL数据库中的时间都是以时间戳的格式来存储时间的,但是对于我们来说,时间戳格式具体表示的是什么时间,我们很难一眼看出来,所以当我们要具体查询某一个时间或时间段的数据时,就要进行日期到时间戳的转换。
    我们常会用到这两个函数:
    FROM_UNIXTIME()和UNIX_TIMESTAMP()函数
    1. FROM_UNIXTIME(unix_timestamp,format)函数:
    FROM_UNIXTIME(unix_timestamp,format)时间函数中unix_timestamp可以是字段名,也可以直接是Unix 时间戳,format主要是将返回值格式化。

    2.UNIX_TIMESTAMP()函数
    UNIX_TIMESTAMP()是与之相对正好相反的时间函数
    UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
      若无参数调用,则返回一个 Unix timestamp (‘1970-01-01 00:00:00’ GMT 之后的秒数) 作为无符号整数。若用date 来调用 UNIX_TIMESTAMP(),它会将参数值以’1970-01-01 00:00:00’ GMT后的秒数的形式返回。date 可以是一个 DATE 字符串、一个 DATETIME字符串、一个 TIMESTAMP或一个当地时间的YYMMDD 或YYYMMDD格式的数字。
      下面有几种情况下的使用:
    (1)、查询当前系统的时间戳

    mysql> select unix_timestamp();
    +------------------+
    | unix_timestamp() |
    +------------------+
    | 1481957775 |
    +------------------+
    1 row in set (0.00 sec)
    1
    2
    (2)、查询当前系统时间格式的时间

    mysql> select from_unixtime(unix_timestamp());
    +---------------------------------+
    | from_unixtime(unix_timestamp()) |
    +---------------------------------+
    | 2016-12-17 14:59:24 |
    +---------------------------------+
    1 row in set (0.00 sec)
    1
    2
    (3)、查询某一固定时间的时间戳

    mysql> select unix_timestamp('2016-12-17 14:59:24');
    +---------------------------------------+
    | unix_timestamp('2016-12-17 14:59:24') |
    +---------------------------------------+
    | 1481957964 |
    +---------------------------------------+
    1 row in set (0.00 sec)
    1
    2
    3
    (4)、查询某一时间戳的固定时间

    mysql> select from_unixtime('1481957964');
    +-----------------------------+
    | from_unixtime('1481957964') |
    +-----------------------------+
    | 2016-12-17 14:59:24 |
    +-----------------------------+
    1 row in set (0.00 sec)
    1
    2
    3
    (5)、查询某一时间戳的具体时间按固定格式输出

    mysql> select from_unixtime('1481957964','%Y/%m/%d %H:%i:%s');
    +-------------------------------------------------+
    | from_unixtime('1481957964','%Y/%m/%d %H:%i:%s') |
    +-------------------------------------------------+
    | 2016/12/17 14:59:24 |
    +-------------------------------------------------+
    1 row in set (0.00 sec)
    1
    2
    (6)、查询某个数据的创建时间大于某个时间段比如(2012-07-08 00:00:11)的数据,同时显示具体的创建时间:

    mysql> SELECT FROM_UNIXTIME(createtime) as '创建时间',FROM_UNIXTIME(`last_modified`) as '更新时间
    -> from orders where createtime > UNIX_TIMESTAMP('2016-07-08 00:00:11');
    +---------------------+---------------------+
    | 创建时间 | 更新时间 |
    +---------------------+---------------------+
    | 2016-07-08 14:25:54 | 2016-07-08 14:31:10 |
    | 2016-07-08 15:37:08 | 2016-07-10 17:26:15 |
    | 2016-07-10 17:25:42 | 2016-07-10 17:26:58 |
    +---------------------+---------------------+
    3 rows in set (0.00 sec)
    1
    2
    3
    (7)mysql 获取当前时间为:

    mysql> select now();
    +---------------------+
    | now() |
    +---------------------+
    | 2017-06-15 16:40:57 |
    +---------------------+
    1 row in set (0.00 sec)
    1
    2
    (8)mysql 获取当前时间戳为:

    mysql> select unix_timestamp(now());
    +-----------------------+
    | unix_timestamp(now()) |
    +-----------------------+
    | 1497516186 |
    +-----------------------+
    1 row in set (0.00 sec)
    ————————————————

  • 相关阅读:
    【转】Foreach用法
    【转】PB实现在通知区域添加图标
    手动清理SQLSERVER的日志文件
    2012腾讯实习笔试
    触控(捕鱼达人)笔试
    C和指针第五章操作符和表达式
    浮点数的存储
    C和指针第三章数据
    动态规划
    有关海量数据处理
  • 原文地址:https://www.cnblogs.com/zhichao123/p/12016692.html
Copyright © 2011-2022 走看看