fuanyu 发表于 2013-1-28 14:36:30

mysql查询时间语句大全

本示例使用mysql5.0
1.准备好表
INSERT INTO a
   (`id`, `password`, `startTime`)
VALUES
   ('11', 'bb', "2010-11-29 11:09:55");
INSERT INTO a
   (`id`, `password`, `startTime`)
VALUES
   ('12', '5566', "2010-12-15 11:10:11");
INSERT INTO a
   (`id`, `password`, `startTime`)
VALUES
   ('13', '222', "2008-12-28 11:34:23");
 
2.查询某年某月某日
 select * from a where  date_format(startTime, '%Y-%m-%d ')= '2010-12-15 '
3.查询最近一天(包括昨天和今天):
select * from a where to_days(startTime) = to_days(now());

select * from a where date(startTime) = curdate();

select * from a where to_days(now())-to_days(startTime)<=1
 
4.查询最近一周:
select * from a where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(startTime);

select * from a where to_days(now())-to_days(startTime)<=7
5.查询最近一个月:
select * from a where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(startTime);
6.查询两个日期之间的语句
select * from a where startTime >'2010-05-31 12:12:12 ' and startTime <'2010-11-09 02:02:02'
 
下面是操作时间函数:
<div class="bct fc05 fc11 nbw-blog ztag js-fs2">当前时间
页: [1]
查看完整版本: mysql查询时间语句大全