
话说有一文章表article,存储文章的添加文章的时间是add_time字段,该字段为int()类型的,现需要查询今天添加的文章总数并且按照时间从大到小排序,则查询语句如下: select * from `article` where date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d');
或者: select * from `article` where to_days(date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d')) = to_days(now());
假设以上表的add_time字段的存储类型是DATETIME类型或者TIMESTAMP类型,则查询语句也可按如下写法: 查询今天的信息记录: select * from `article` where to_days(`add_time`) = to_days(now());
查询昨天的信息记录: select * from `article` where to_days(now()) – to_days(`add_time`) <= ;
查询近7天的信息记录: select * from `article` where date_sub(curdate(), INTERVAL DAY) <= date(`add_time`);
查询近30天的信息记录: select * from `article` where date_sub(curdate(), INTERVAL DAY) <= date(`add_time`);
查询本月的信息记录: select * from `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');
查询上一月的信息记录: select * from `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =;
对上面的SQL语句中的几个函数做一下分析: ()to_days 就像它的名字一样,它是将具体的某一个日期或时间字符串转换到某一天所对应的unix时间戳,如: mysql> select to_days('2010-11-22 14:39:51');
+--------------------------------+
| to_days('2010-11-22 14:39:51') |
+--------------------------------+
| |
+--------------------------------+ mysql> select to_days('2010-11-23 14:39:51');
+--------------------------------+
| to_days('2010-11-23 14:39:51') |
+--------------------------------+
| |
+--------------------------------+
可以看出22日与23日的差别就是,转换之后的数增加了1,这个粒度的查询是比较粗糙的,有时可能不能满足我们的查询要求,那么就需要使用细粒度的查询方法str_to_date函数了,下面将分析这个函数的用法。 提醒: ()to_days() 不用于阳历出现()前的值,原因是当日历改变时,遗失的日期不会被考虑在内。因此对于1582 年之前的日期(或许在其它地区为下一年 ), 该函数的结果实不可靠的。 ()MySQL"日期和时间类型"中的规则是将日期中的二位数年份值转化为四位。因此对于'1997-10-07'和'97-10-07'将被视为同样的日期: mysql> select to_days('1997-10-07'), to_days('97-10-07'); -> ,
()str_to_date 这个函数可以把字符串时间完全的翻译过来,如: mysql> select str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s'); +--------------------------------------------------------+
| str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s') |
+--------------------------------------------------------+
| -- :: |
+--------------------------------------------------------+
具体案例操作如下: select str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')
from article
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';
查询
今天
select * from 表名 where to_days(时间字段名) = to_days(now());
昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 时间字段名) <=
7天 SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL DAY) <= date(时间字段名)
近30天 SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 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’ ) ) =
同时,再附上 一个 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 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 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())-;
查询当前月份的数据
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 month) and now();
查询上个月的数据
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 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
LIMIT ,