zoukankan      html  css  js  c++  java
  • mysql查询时间戳和日期的转换

     
    数据库的使用中,经常需要按指定日期来查询记录,以便于统计,而在数据库中,有很多存储的是时间戳,
     
    也有的直接存日期,查询的时候可能不是那么好弄.
     
    mysql提供了两个函数:
     
              from_unixtime(time_stamp)   ->  将时间戳转换为日期
     
              unix_timestamp(date)             ->  将指定的日期或者日期字符串转换为时间戳
     
     
    如:   from_unixtime(time_stamp)
    [plain]
    select from_unixtime(1382544000); 
    +---------------------------+ 
    | from_unixtime(1382544000) | 
    +---------------------------+ 
    | 2013-10-24 00:00:00       | 
    +---------------------------+ 
    如: unix_timestamp(date)
     
    [plain]
    select unix_timestamp(date('2013-10-24')); 
    +------------------------------------+ 
    | unix_timestamp(date('2013-10-24')) | 
    +------------------------------------+ 
    |                         1382544000 | 
    +------------------------------------+ 
     
    如果要查询当天的订单的记录:
    [plain]
    select count(*) from b_order Where  date_format(from_unixtime(create_time),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d'
     
    也可以这样:
    [plain]
    select count(*) from b_order Where  create_time >= unix_timestamp('2013-10-24 00:00:00') and create_time <=  unix_timestamp('2013-10-24 23:59:59') ; 
     例:
    SELECT NOW(),UNIX_TIMESTAMP()
    
    UPDATE `order` SET `status` = 'closed' WHERE `pay_status` = 'wait_pay' AND UNIX_TIMESTAMP()>create_time+172800
    
    SELECT create_time+172800,date_add(now(), interval 1 hour),FROM_UNIXTIME(create_time,'%Y%m%d %H'), FROM_UNIXTIME(create_time,'%Y%m%d %H') FROM `order`
  • 相关阅读:
    python发送邮件
    常用的排序算法
    关于前端ajax请求url为何添加一个随机数
    RabbitMQ消息队列
    shell编程基本语法和变量
    第70课 展望:未来的学习之路(完结)
    第69课 技巧:自定义内存管理
    第68课 拾遗:让人迷惑的写法
    第67课 经典问题解析五
    第66课 C++中的类型识别
  • 原文地址:https://www.cnblogs.com/niuben/p/11159078.html
Copyright © 2011-2022 走看看