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

來源:互聯網
上載者:User

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

最近項目中用到了查詢當月資料記錄的功能,最初的想法是在邏輯業務裡構造好時間段進行查詢,當寫sql語句時感覺挺麻煩。所以就到網上搜尋了一下,看看是不是能有簡單的方法。果然,網路資源很強大。下面結合我的項目表來把mysql查詢今天,昨天,近7天,近30天,本月,上一月資料的sql語句記錄一下。

有一張ad_proTrack_t 表,追蹤產品時間欄位為crt_time

查詢今天的資訊記錄:

select * from ad_proTrack_t  where to_days(`crt_time`) = to_days(now());
查詢昨天的資訊記錄:
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;


ASPNET中怎查詢今天,昨天,最近7天,最近一個月的記錄?(access資料庫)

select * from tb_user where datediff('d',log_time,now)=0
select * from tb_user where datediff('d',log_time,now)=1
select * from tb_user where datediff('d'log_time,now)=7
select * from tb_user where datediff('d'log_time,now)=30
 
MYSQL查詢一周內的資料(最近7天的)怎寫

select * from wap_content where week(created_at) = week(now)

如果你要嚴格要求是某一年的,那可以這樣

查詢一天:

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 7 DAY) <= date(column_time);

查詢一個月:

select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time);
 

相關文章

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.