日期:2014-05-16  浏览次数:20672 次

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)