mysql 常見時間條件查詢

2021-07-01 23:38:06 字數 1416 閱讀 8336

查詢今天的資訊記錄:

select * from `article` where to_days(`add_time`) = to_days(now());

查詢昨天的資訊記錄:

select * from `article` where to_days(now()) – to_days(`add_time`) <= 1;

查詢近7天的資訊記錄:

select * from `article` where date_sub(curdate(), interval 7 day) <= date(`add_time`);

查詢近30天的資訊記錄:

select * from `article` where date_sub(curdate(), interval 30 day) <= date(`add_time`);

查詢本月的資訊記錄:

select * from `article` where date_format(`add_time`, 『%y%m') = date_format(curdate() , 『%y%m');

查詢上一月的資訊記錄:

select * from `article` where period_diff(date_format(now() , 『%y%m') , date_format(`add_time`, 『%y%m')) =1;

查詢當前這週的資料 

select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now()); 

查詢上週的資料 

select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now())-1; 

查詢本季度資料 

select * from `ht_invoice_information` where quarter(create_date)=quarter(now()); 

#查詢上季度資料 

select * from `ht_invoice_information` where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter)); 

#查詢本年資料 

select * from `ht_invoice_information` where year(create_date)=year(now()); 

#查詢上年資料 

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year)) 

mysql查詢條件 Mysql查詢條件的使用

mysql查詢條件的使用 方法 解釋 gt 大於 gte 大於等於 lt 小於 lte 小於等於 例如 article article.objects.filter id gt 5 startswith 以指定某個字串開始,大小寫敏感 istartswith 以指定某個字串開始,大小寫不敏感 end...

sql 時間條件查詢

select from table t where t.time to date aaaa,yyyy mm dd hh24 mm ss and t.timeaaaa,bbbb是字串型別 比如 aaaa 2018 04 19 00 00 00 bbbb 2018 04 20 00 00 00 to d...

sql 時間條件查詢

select from table t where t.time to date aaaa,yyyy mm dd hh24 mm ss and t.timeaaaa,bbbb是字串型別 比如 aaaa 2018 04 19 00 00 00 bbbb 2018 04 20 00 00 00 to d...