Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2793253
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: Mysql/postgreSQL

2014-02-12 09:30:37

在数据库的使用中,经常需要按指定日期来查询记录,以便于统计,而在数据库中,有很多存储的是时间戳,

也有的直接存日期,查询的时候可能不是那么好弄.

mysql提供了两个函数:

          from_unixtime(time_stamp)   ->  将时间戳转换为日期

          unix_timestamp(date)             ->  将指定的日期或者日期字符串转换为时间戳

也可以指定格式
 
SELECT FROM_UNIXTIME(1382544000,'%Y-%m-%d')  AS a

如:   from_unixtime(time_stamp)

[plain] view plaincopy
  1. select from_unixtime(1382544000);  
  2. +---------------------------+  
  3. | from_unixtime(1382544000) |  
  4. +---------------------------+  
  5. | 2013-10-24 00:00:00       |  
  6. +---------------------------+  

如: unix_timestamp(date) 


[plain] view plaincopy
  1. select unix_timestamp(date('2013-10-24'));  
  2. +------------------------------------+  
  3. | unix_timestamp(date('2013-10-24')) |  
  4. +------------------------------------+  
  5. |                         1382544000 |  
  6. +------------------------------------+  


如果要查询当天的订单的记录:
[plain] view plaincopy
  1. select count(*) from b_order Where  date_format(from_unixtime(create_time),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')  
也可以这样:
[plain] view plaincopy
  1. 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') ;  
阅读(1739) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~