mysql查詢今天,昨天,近7天,近30天,本月,上一月資料的SQL

來源:互聯網
上載者:User

標籤:int   class   crt   for   _for   rda   date_sub   diff   s函數   

原文:http://www.open-open.com/code/view/1423207309170

 

select * from ad_proTrack_t  where to_days(crt_time) = to_days(now());//今天做測試的時候調用到了這句sql,發現不是想要的結果。經過嘗試發現,to_days函數括弧內的‘時間欄位’不能加引號,加引號的轉換後為NULL查詢昨天的資訊記錄: select * from ad_proTrack_t where to_days(now()) – to_days(crt_time) <= 1;查詢近7天的資訊記錄: select * from ad_proTrack_t where date_sub(curdate(), INTERVAL 7 DAY) <= date(crt_time);查詢近30天的資訊記錄: select * from ad_proTrack_t  where date_sub(curdate(), INTERVAL 30 DAY) <= date(crt_time);查詢本月的資訊記錄: select * from ad_proTrack_t  where date_format(crt_time, ‘%Y%m‘) = date_format(curdate() , ‘%Y%m‘);查詢上一月的資訊記錄:select * from ad_proTrack_t where period_diff(date_format(now() , ‘%Y%m‘) , date_format(crt_time, ‘%Y%m‘)) =1;

 

mysql查詢今天,昨天,近7天,近30天,本月,上一月資料的SQL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.