MySQL里获取当前week、month、quarter的start_date/end_date

当前week第一天
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 1 DAY) 

当前week最后一天
select date_sub(curdate(),INTERVAL WEEKDAY(curdate())-1 DAY)

前一week的第一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 8 DAY) 


前一week的最后一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 2 DAY) 


前两week的第一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 15 DAY) 


前两week的最后一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 9 DAY) 

获取本周内的数据:
 create_date>=date_sub(now(),INTERVAL WEEKDAY(now()) + 1 day) " +
	    						"and create_date<=date_sub(now(),INTERVAL WEEKDAY(now()) - 5 DAY)

当前month的第一天:
SELECT concat(date_format(LAST_DAY(now()),'%Y-%m-'),'01') 


当前month的最后一天:
SELECT LAST_DAY(now())

查询当前月内的数据
SELECT * from resource_article where  create_date>=concat(date_format(LAST_DAY(now()),'%Y-%m- %T'),'01') and create_date<=LAST_DAY(now()) ;


前一month的第一天:
SELECT concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01') 


前一month的最后一天:
SELECT LAST_DAY(now() - interval 1 month) 


前两month的第一天:
SELECT concat(date_format(LAST_DAY(now() - interval 2 month),'%Y-%m-'),'01') 


前两month的最后一天:
SELECT LAST_DAY(now() - interval 2 month) 


季度:
当前quarter的第一天:
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-3 month),'%Y-%m-'),'01') 


当前quarter的最后一天:
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-1 month) 


前一quarter的第一天:
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-6 month),'%Y-%m-'),'01') 


前一quarter的最后一天:
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-4 month) 


前两quarter的第一天:
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-9 month),'%Y-%m-'),'01') 


前两quarter的最后一天:
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-7 month)

你可能感兴趣的:(mysql)