话说有一文章表article,存储文章的增加文章的工夫是add_time字段,该字段为int(5)类型的,现须要查问明天增加的文章总数并且依照工夫从大到小排序,则查问语句如下: 02
03 1 select * from `article` where date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d'); 04 或者: 05
06 1 select * from `article` where to_days(date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d')) = to_days(now()); 07 假如以上表的add_time字段的存储类型是DATETIME类型或者TIMESTAMP类型,则查问语句也可按如下写法: 08
09 查问明天的信息记录: 10
11 1 select * from `article` where to_days(`add_time`) = to_days(now()); 12 查问昨天的信息记录: 13
14 1 select * from `article` where to_days(now()) – to_days(`add_time`) <= 1; 15 查问近7天的信息记录: 16
17 1 select * from `article` where date_sub(curdate(), INTERVAL 7 DAY) <= date(`add_time`); 18 查问近30天的信息记录: 19
20 1 select * from `article` where date_sub(curdate(), INTERVAL 30 DAY) <= date(`add_time`); 21 查问本月的信息记录: 22
23 1 select * from `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m'); 24 查问上一月的信息记录: 25
26 1 select * from `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =1; 27 对下面的SQL语句中的几个函数做一下剖析: 28
29 (1)to_days 30
31 就像它的名字一样,它是将具体的某一个日期或工夫字符串转换到某一天所对应的unix工夫戳,如: 32
33 01 mysql> select to_days('2010-11-22 14:39:51'); 34 02 +--------------------------------+
35 03 | to_days('2010-11-22 14:39:51') |
36 04 +--------------------------------+
37 05 | 734463 |
38 06 +--------------------------------+
39 07
40 08 mysql> select to_days('2010-11-23 14:39:51'); 41 09 +--------------------------------+
42 10 | to_days('2010-11-23 14:39:51') |
43 11 +--------------------------------+
44 12 | 734464 |
45 13 +--------------------------------+
46 能够看出22日与23日的差异就是,转换之后的数减少了1,这个粒度的查问是比拟毛糙的,有时可能不能满足咱们的查问要求,那么就须要应用细粒度的查询方法str_to_date函数了,上面将剖析这个函数的用法。 47
48 揭示: 49
50 (1)to_days() 不用于阳历呈现(1582)前的值,起因是当日历扭转时,遗失的日期不会被思考在内。因而对于1582 年之前的日期(或者在其它地区为下一年 ), 该函数的结果实不牢靠的。 51
52 (2)MySQL"日期和工夫类型"中的规定是将日期中的二位数年份值转化为四位。因而对于'1997-10-07'和'97-10-07'将被视为同样的日期: 53
54 1 mysql> select to_days('1997-10-07'), to_days('97-10-07'); 55 2
56 3 -> 729669, 729669
57 (2)str_to_date 58
59 这个函数能够把字符串工夫齐全的翻译过去,如: 60
61 1 mysql> select str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s'); 62 2
63 3 +--------------------------------------------------------+
64 4 | str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s') |
65 5 +--------------------------------------------------------+
66 6 | 2010-11-23 14:39:51 |
67 7 +--------------------------------------------------------+
68 具体案例操作如下: 69
70 1 select str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s') 71 2 from article 72 3 where str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')>='2012-06-28 08:00:00' and str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')<='2012-06-28 09:59:59';
[](javascript:void(0); "复制代码")
[](javascript:void(0); "复制代码")
查问
明天 select * from 表名 where to_days(工夫字段名) = to_days(now());
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 工夫字段名) <= 1 7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(工夫字段名)
近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(工夫字段名)
本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 工夫字段名, ‘%Y%m’ ) = DATE_FORMAT( CURDATE( ) , ‘%Y%m’ )
上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m’ ) , date_format( 工夫字段名, ‘%Y%m’ ) ) =1 同时,再附上 一个 mysql官网的相干document
#查问本季度数据 select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
#查问上季度数据 select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#查问本年数据 select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
#查问上年数据 select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
查问以后这周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
查问上周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查问以后月份的数据 select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查问间隔以后当初6个月的数据 select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查问上个月的数据 select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m') select * from ` user ` where DATE_FORMAT(pudate, ' %Y%m ' ) = DATE_FORMAT(CURDATE(), ' %Y%m ' ) ; select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now()) select *
from user where MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now()) select *
from [ user ] where YEAR (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = YEAR (now())
and MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now()) select *
from [ user ] where pudate between 上月最初一天
and 下月第一天 where date(regdate) = curdate(); select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
SELECT date( c_instime ) ,curdate( )
FROM `t_score`
WHERE 1 LIMIT 0 , 30
发表回复