文档章节

Mysql查询当天,周,月数据

风神AI
 风神AI
发布于 2017/01/16 15:47
字数 417
阅读 5
收藏 0

假设table里有个字段叫addTime 记录的是数据添加的时间

SELECT * FROM Table_Name WHERE date(addTime) = curdate();
NOW() CURDATE() CURTIME()
2008-12-29 16:25:46 2008-12-29 16:25:46

 补充:本周、上周、本月、上个月份的数据

  查询当前这周的数据

  代码如下

 

 SELECT * FROM TABLE_NAME WHERE YEARWEEK(date_format(addTime,'%Y-%m-%d')) = YEARWEEK(now());

  查询上周的数据

  代码如下

 

 SELECT * FROM TABLE_NAME WHERE YEARWEEK(date_format(addTime,'%Y-%m-%d')) = YEARWEEK(now())-1;

  查询当前月份的数据

 

 select * from TABLE_NAME where date_format(addTime,'%Y-%m')=date_format(now(),'%Y-%m')

  查询距离当前现在6个月的数据

  代码如下

 

 select * from TABLE_NAME where addTime between date_sub(now(),interval 6 month) and now();

  查询上个月的数据

  代码如下

  select * from TABLE_NAME where date_format(addTime,'%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的秒数