当前位置:首页 > PHP教程 > PHP总结归纳

mysql查询当天所有数据sql语句

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()

  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();

  另一种写法没测试过

  查询当天的记录

  代码如下

  select * from hb_article_view where to_days(hb_addtime) = to_days(now())

  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 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 下月第一天

  mysql查询多少秒内的数据

  代码如下

  select count( * ) as c, sum( if( logusertype =2, logusertype, 0 ) ) /2 as a, sum( if( logusertype =3, logusertype, 0 ) ) /3 as b

  from testlog where unix_timestamp(now())-unix_timestamp( logendtime )<=30

  查询30秒内记录的总数,loguser等于2的记录的总数和,和 loguser等于3的记录的总数.

  if( logusertype =2, logusertype, 0 ) 如果logusetype等于2 就在logusertype上累加,否则加0。

  sum( if( logusertype =2, logusertype, 0 ) ) 把logusertype都累加起来。

  sum( if( logusertype =2, logusertype, 0 ) ) /2 as a, 除以2是统计个数。

  unix_timestamp(now())计算当前时间的秒数,

  unix_timestamp( logendtime )计算logendtime的秒数

,.syntaxhighlighter{padding-top:20px;padding-bottom:20px;}
【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!