当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql中获取一天、一周、一月时间数据的各种sql语句写法

mysql中获取一天、一周、一月时间数据的各种sql语句写法

2017年12月12日  | 移动技术网IT编程  | 我要评论

创建表:

复制代码 代码如下:

create table if not exists t
(
   id int,
   addtime datetime default '0000-00-00 00:00:00′
)

添加两条初始数据:

insert t values(1, '2012-07-12 21:00:00′);
insert t values(2, '2012-07-22 21:00:00′);

一、当天或当日插入的数据:

1、传统对比判断:select * from `t` where date_format(addtime,'%y-%m-%d') = date_format(now(),'%y-%m-%d')");
2、第一点的简写:select * from `t` where addtime >= date_format(now(),'%y-%m-%d');
3、利用函数判断:select * from `t` where datediff(addtime,now()) =0;//推荐此方法
4、利用时间戳判断:select * from `t` where addtime between (unix_timestamp(now()-86440)) and now();
注:返回当天时间的mysql函数有curdate()、current_date()、current_date、now()几种;其中now()获取的日期格式为0000-00-00 00:00:00的时间;curdate()、current_date()、current_date是获取日期格式为0000-00-00的时间,所以返回的时间无时分秒;

二、当月的相关数据写法:

1、今天是当月的第几天:select dayofmonth( now( ) );
2、获取当月的第一天时间:select data_sub( now(), interval dayofmonth(now()) – 1 day);
日期运算函数,句型:date_add(date,interval expr type)和date_sub(date,interval expr type)
date为一个datetime或date值,可当作起始时间,expr为一个表达式,用来指定增加或减少的时间间隔数目,可为一个字符串–若为负值,则在表达式前加个"-"符号。type为关键词,它表示了表达式被解释的方式,通常用年(year)、月(month)、日(day)、周(week)等。
interval的用户说明:

1、当函数使用时,即interval(),为比较函数,如:interval(10,1,3,5,7); 结果为4;
原理:10为被比较数,后面1,3,5,7为比较数,将后面四个依次与10比较,看后面数字组有多少个少于10,则返回其个数。前提是后面数字组为从小到大排列,否则返回结果0。
2、当关键词使用时,表示为设置时间间隔,常用在date_add()与date_sub()函数里,如:interval 1 day ,解释为将时间间隔设置为1天。

弄清楚了上面几个重要的日期运算函数,下面再来一个混合的日期运算。
3、上个月的第一天时间:select date_sub( date_sub( now( ) , interval dayofmonth( now( ) ) -1 day ) , interval 1 month ); 是不是一目了然了!

三、当周的相关数据写法:

1、获取今天是一周第几天或星期几:select weekday(now());返回的是数字:0为周一,6为周日
2、获取本周的第一天日期:select date_sub(now(),interval weekday(now()) day);或select date_add(now(),interval -weekday(now()) day);或 select curdate( ) – weekday( curdate( ) );
3、再写一个上周的第一天至现在的数据:(以表t为数据表)
select * from `t` where addtime >= date_format(date_sub(date_sub(now(), interval weekday(now()) day), interval 1 week), ‘%y-%m-%d');是不是有些感觉了!

注:若你是以时间戳保存的时间字段,那么请用from_unixtime()函数转换为时间格式,如:from_unixtime($timestamp)

四、mysql中将日期转为时间戳

前三点的方法,讲的都是将日期转为相应时间常量,并没有提到时间戳与时间的转换,这里把它写出来,

1、时间戳转日期,方法是select from_unixtime(1336542121);
2、日期转时间戳,方法是:select unix_timestamp('2013-04-08′);

结合前面3点,对于时间戳就更能灵活运用了!最后更新于:2013.04.08

mysql查询 昨天 一周前 一月前 一年前的数据

mysql 昨天 一周前 一月前 一年前的数据 这里主要用到了date_sub,

参考如下

复制代码 代码如下:

select * from yh_content
where inputtime>date_sub(curdate(), interval 1 day)
where inputtime>date_sub(curdate(), interval 1 week)
where inputtime>date_sub(curdate(), interval 1 month)
where inputtime>date_sub(curdate(), interval 1 year)

注意:如果数据库中时间以unix时间戳的形式存放的话,在时间对比上需要更改为统一格式:

date_sub()返回的是格式化后的时间:2014-05-17

需要用unix_timestamp()转化为unix时间戳形式对比:

复制代码 代码如下:

where inputtime>unix_timestamp(date_sub(curdate(), interval 1 day))
where inputtime>unix_timestamp(date_sub(curdate(), interval 1 week))
where inputtime>unix_timestamp(date_sub(curdate(), interval 1 month))
where inputtime>unix_timestamp(date_sub(curdate(), interval 1 year))

详细请查看mysql时间函数:date_sub、date_add、unix_timestamp等函数的用法

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网