SELECT COUNT(*) FROM `table` GROUP BY FROM_UNIXTIME(time,'%Y');
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、邕寧ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的邕寧網(wǎng)站制作公司
SELECT COUNT(*) FROM `table` GROUP BY FROM_UNIXTIME(time,'%m');
SELECT COUNT(*) FROM `table` GROUP BY FROM_UNIXTIME(time,'%d');
上面分別是按年,月,日來(lái)分組的,我不知道你的時(shí)間存儲(chǔ)的是什么類(lèi)型,我這個(gè)是按時(shí)間戳來(lái)的。
您好,一、年度查詢(xún)
查詢(xún) 本年度的數(shù)據(jù)
SELECT *
FROM blog_article
WHERE year( FROM_UNIXTIME( BlogCreateTime ) ) = year( curdate( ))
二、查詢(xún)季度數(shù)據(jù)
查詢(xún)數(shù)據(jù)附帶季度數(shù)
SELECT ArticleId, quarter( FROM_UNIXTIME( `BlogCreateTime` ) )
FROM `blog_article`
其他的同前面部分:查詢(xún) 本季度的數(shù)據(jù)
SELECT *
FROM blog_article
WHERE quarter( FROM_UNIXTIME( BlogCreateTime ) ) = quarter( curdate( ))
三、查詢(xún)?cè)露葦?shù)據(jù)
本月統(tǒng)計(jì)(MySQL)
select * from booking where month(booking_time) =
month(curdate()) and year(booking_time) = year(curdate())
本周統(tǒng)計(jì)(MySQL)
select * from spf_booking where month(booking_time) =
month(curdate()) and week(booking_time) = week(curdate())
四、時(shí)間段
N天內(nèi)記錄
WHERE TO_DAYS(NOW()) - TO_DAYS(時(shí)間字段) = N
當(dāng)天的記錄
where date(時(shí)間字段)=date(now())
或
where to_days(時(shí)間字段) = to_days(now());
查詢(xún)一周:
select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) = date(column_time);
查詢(xún)一個(gè)月:
select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) = date(column_time);
查詢(xún)'06-03'到'07-08'這個(gè)時(shí)間段內(nèi)所有過(guò)生日的會(huì)員:
Select * From user Where
DATE_FORMAT(birthday,'%m-%d') = '06-03' and DATE_FORMAT(birthday,'%m-%d')
= '07-08';
統(tǒng)計(jì)一季度數(shù)據(jù),表時(shí)間字段為:savetime
group by concat(date_format(savetime, '%Y '),FLOOR((date_format(savetime, '%m ')+2)/3))
或
select YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1,count(*)
from yourTable
group by YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1;
五、分組查詢(xún)
1、年度分組
2、月度分組
3、先按年度分組,再按月度分組
4、按年月分組
SELECT count(ArticleId), date_format(FROM_UNIXTIME( `BlogCreateTime`),'%y%m') sdate FROM `blog_article` group by sdate
結(jié)果:
count( ArticleId ) sdate
17 0901
11 0902
5 0903
6 0904
2 0905
1 0907
12 0908
6 0909
11 0910
3 0911
select
table.year,
table.month,
table.day
from
(SELECT
year(time) year,
month(time) month,
day(time) day
FROM
table) table
group by table.year,table.month,table.day
order BY table.year,table.month,table.day desc;
望采納,有疑問(wèn)或是有更好寫(xiě)法,請(qǐng)多交流