mysql 时间 本周 本月_MySQL日期查询:本周、本月、本季、本年
查詢當天
SELECT * FROM 表名 WHERE TO_DAYS(時間字段名) = TO_DAYS(now());
查詢昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 時間字段名) <= 1
查詢一周
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(時間字段名)
查詢近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(時間字段名)
查詢本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 時間字段名, ‘%Y%m’ ) = DATE_FORMAT( CURDATE( ) , ‘%Y%m’ )
查詢上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m’ ) , date_format( 時間字段名, ‘%Y%m’ ) ) =1
查詢本周
select * from ht_invoice_information where WEEKOFYEAR(create_date)=WEEKOFYEAR(NOW());
查詢上周
select * from ht_invoice_information where create_date>=date_add(now(),interval -(8 + weekday(now())) day) and create_date<=date_add(now(),interval -(1 + weekday(now())) day);
或者
select * from `ht_invoice_information` where WEEKOFYEAR(create_date)=WEEKOFYEAR(DATE_SUB(now(),INTERVAL 1 week));
查詢本月
select * from ht_invoice_information where MONTH(create_date)=MONTH(NOW()) and year(create_date)=year(now());
查詢上月
select * from ht_invoice_information where create_date<=last_day(date_add(now(),interval -1 MONTH)) and create_date>=DATE_FORMAT(concat(extract(year_month from date_add(now(),interval -1 MONTH)),’01′),’%Y-%m-%d’);
或者
select * from `ht_invoice_information` where MONTH(create_date)=MONTH(DATE_SUB(NOW(),interval 1 month))
and year(create_date)=year(now());
查詢本季度
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日期查询:本周、本月、本季、本年的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 计算机行业研究及2020年策略:聚焦主赛
- 下一篇: tiptop使用java的poi包实现E
