這篇文章主要介紹“MySQL如何查詢近一周的數(shù)據(jù)”,在日常操作中,相信很多人在mysql如何查詢近一周的數(shù)據(jù)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”mysql如何查詢近一周的數(shù)據(jù)”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
創(chuàng)新互聯(lián)成都企業(yè)網(wǎng)站建設服務,提供成都做網(wǎng)站、網(wǎng)站建設網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設計,成都響應式網(wǎng)站建設,網(wǎng)頁設計師打造企業(yè)風格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務。歡迎咨詢做網(wǎng)站需要多少錢:028-86922220
在mysql中,可以利用select語句查詢近一周的數(shù)據(jù),語法為“select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);”。
本教程操作環(huán)境:windows10系統(tǒng)、mysql8.0.22版本、Dell G3電腦。
語法如下:
select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);
拓展知識:
查詢一天:
select * from table where to_days(column_time) = to_days(now()); select * from table where date(column_time) = curdate();
查詢一個月:
select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time);
示例如下:
效果如圖(這里SQL語句中的一周范圍是指星期一到星期日,星期一為一周的第一天,因是8月11日查詢的,所以只顯示星期一到星期六的結(jié)果):
日歷:
簡單來說就是用今天的日期生成前七天的日期(利用union all命令),并根據(jù)星期一的日期條件刷選出本周的日期
SELECT DATE(subdate(curdate(),date_format(curdate(),'%w')-1)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 1 day)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 2 day)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 3 day)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 4 day)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 5 day)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 6 day)) as thisweek
解析:
SELECT DATE(subdate(curdate(),date_format(curdate(),’%w’)-1))
得到的是這周的第一天(星期一到星期天為一周);也即8月6日
到此,關(guān)于“mysql如何查詢近一周的數(shù)據(jù)”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
本文名稱:mysql如何查詢近一周的數(shù)據(jù)
文章URL:http://aaarwkj.com/article18/jeiggp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、全網(wǎng)營銷推廣、微信公眾號、軟件開發(fā)、網(wǎng)站內(nèi)鏈、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)