mysql按照日期查询

  • baagee 发布于 2017-04-14 18:56:21
  • 分类:MySQL
  • 1650 人围观
  • 2 人喜欢

mysql查询当天的所有信息:

  代码如下

select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())

这个有一些繁琐,还有简单的写法:

  代码如下

select * from table where date(regdate) = curdate();

date()函数获取日期部分, 扔掉时间部分,然后与当前日期比较即可

本周、上周、本月、上个月份的数据

  查询当前这周的数据

  代码如下

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 table 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 table where YEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = YEAR(now()) and MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())
select * from table where pudate between 上月最后一天 and 下月第一天

mysql查询多少秒内的数据

  代码如下

SELECT count( * ) AS c FROM table WHERE UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP( logendtime )<=30

查询30秒内记录的总数,UNIX_TIMESTAMP(NOW())计算当前时间的秒数,  UNIX_TIMESTAMP( logendtime )计算logendtime的秒数


转载请说明出处:baagee博客 » mysql按照日期查询
标签: MySQL 日期查询

评论

点击图片切换
还没有评论,快来抢沙发吧!