select * from sensor_info where hour(record_date)>=8 and hour(record_date)<=12 and d_type = 'wenshi' and TO_DAYS(now())-TO_DAYS(record_date)<=1 and TO_DAYS(now())-TO_DAYS(record_date)>0;
SELECT d_value FROM sensor_info WHERE YEARWEEK(date_format(record_date,'%Y-%m-%d'))= YEARWEEK(now())-1 and d_type = ‘wenshi’
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')
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 下月第一天
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的秒数
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:mysql获取历史记录