您好,一、年度查詢
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),仁化企業(yè)網(wǎng)站建設(shè),仁化品牌網(wǎng)站建設(shè),網(wǎng)站定制,仁化網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,仁化網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
查詢 本年度的數(shù)據(jù)
SELECT *
FROM blog_article
WHERE year( FROM_UNIXTIME( BlogCreateTime ) ) = year( curdate( ))
二、查詢季度數(shù)據(jù)
查詢數(shù)據(jù)附帶季度數(shù)
SELECT ArticleId, quarter( FROM_UNIXTIME( `BlogCreateTime` ) )
FROM `blog_article`
其他的同前面部分:查詢 本季度的數(shù)據(jù)
SELECT *
FROM blog_article
WHERE quarter( FROM_UNIXTIME( BlogCreateTime ) ) = quarter( curdate( ))
三、查詢月度數(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());
查詢一周:
select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) = date(column_time);
查詢一個(gè)月:
select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) = date(column_time);
查詢'06-03'到'07-08'這個(gè)時(shí)間段內(nèi)所有過生日的會(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;
五、分組查詢
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 COUNT(*) FROM `table` GROUP BY FROM_UNIXTIME(time,'%Y');
SELECT COUNT(*) FROM `table` GROUP BY FROM_UNIXTIME(time,'%m');
SELECT COUNT(*) FROM `table` GROUP BY FROM_UNIXTIME(time,'%d');
上面分別是按年,月,日來分組的,我不知道你的時(shí)間存儲(chǔ)的是什么類型,我這個(gè)是按時(shí)間戳來的。
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;
望采納,有疑問或是有更好寫法,請多交流
可以加個(gè)時(shí)間維度表,以時(shí)間維度表為主表?。╥d,month)id 是和你的表左連接,month是1到12月,一樓說的幾個(gè)方法!應(yīng)該不好實(shí)現(xiàn)的吧?不用寫存儲(chǔ)過程那么復(fù)雜!舉個(gè)例子
select a.rq,
b.xssr
from date_table a left jion table1 b
on a.id=b.id