在mysql中查詢當(dāng)天數(shù)據(jù)我們使用到如year,month,day函數(shù)是一種做法,還有一種利用date(regdate) = curdate()函數(shù),當(dāng)然我們也可以用其它方法,下面我來總結(jié)一下。
mysql查詢當(dāng)天的所有信息,代碼如下:
select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
這個(gè)有一些繁瑣,還有簡單的寫法,代碼如下:
select * from table where date(regdate) = curdate();
另一種寫法沒測試過,查詢當(dāng)天的記錄,代碼如下:
select * from hb_article_view where TO_DAYS(hb_AddTime) = TO_DAYS(NOW())
date()函數(shù)獲取日期部分,扔掉時(shí)間部分,然后與當(dāng)前日期比較即可.
補(bǔ)充:本周、上周、本月、上個(gè)月份的數(shù)據(jù).
查詢當(dāng)前這周的數(shù)據(jù),代碼如下:
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
查詢上周的數(shù)據(jù),代碼如下:
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查詢當(dāng)前月份的數(shù)據(jù):
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查詢距離當(dāng)前現(xiàn)在6個(gè)月的數(shù)據(jù),代碼如下:
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查詢上個(gè)月的數(shù)據(jù),代碼如下:
- 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 * --Vevb.com
- 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查詢多少秒內(nèi)的數(shù)據(jù),代碼如下:
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秒內(nèi)記錄的總數(shù),loguser等于2的記錄的總數(shù)和,和 loguser等于3的記錄的總數(shù).
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是統(tǒng)計(jì)個(gè)數(shù)。
UNIX_TIMESTAMP(NOW())計(jì)算當(dāng)前時(shí)間的秒數(shù).
UNIX_TIMESTAMP( logendtime )計(jì)算logendtime的秒數(shù).
新聞熱點(diǎn)
疑難解答
圖片精選