首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

mysql时间查询,该如何解决

2012-08-01 
mysql时间查询mysql 5.0create table `actives` (`starttime` int(11) not null default 0,//此字段为ph

mysql时间查询
mysql 5.0

create table `actives` (
  `starttime` int(11) not null default '0',//此字段为php时间戳
) engine=myisam default charset=gbk;

insert into `actives` values ('1342658714');
insert into `actives` values ('1342858714');
insert into `actives` values ('1343658714');
insert into `actives` values ('1344658714');
insert into `actives` values ('1345658714');
insert into `actives` values ('1346658714');

需要统计出3种类型的结果,
1,查询starttime大于当前服务器时间,并且在 三天内的结果
2,查询starttime大于当前服务器时间,并且在 一周内的结果
2,查询starttime大于当前服务器时间,并且在 一月内的结果

[解决办法]
1,查询starttime大于当前服务器时间,并且在 三天内的结果

SQL code
select * from actives where starttime between UNIX_TIMESTAMP() and  UNIX_TIMESTAMP() + 3*24*60*60;
[解决办法]

mysql> select date_add(now(),interval -1 day); 
+---------------------------------+
| date_add(now(),interval -1 day) |
+---------------------------------+
| 2012-07-23 13:20:24 |
+---------------------------------+
1 row in set (0.00 sec)

mysql> select date_add(now(),interval -1 week);
+----------------------------------+
| date_add(now(),interval -1 week) |
+----------------------------------+
| 2012-07-17 13:20:29 |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select date_add(now(),interval -1 month);
+-----------------------------------+
| date_add(now(),interval -1 month) |
+-----------------------------------+
| 2012-06-24 13:20:32 |
+-----------------------------------+
1 row in set (0.00 sec)

热点排行