MySQL花式查询集合

  1. 今天(自然天)
	SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00') AS '今天开始';
	SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 23:59:59') AS '今天结束';
  1. 昨天(自然天)
	SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 DAY), '%Y-%m-%d 00:00:00') AS '昨天开始';
	SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 DAY), '%Y-%m-%d 23:59:59') AS '昨天结束';
  1. 本周(自然周)
	SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), '%Y-%m-%d 00:00:00') AS '本周一';
	SELECT DATE_FORMAT( DATE_ADD(SUBDATE(CURDATE(), WEEKDAY(CURDATE())), INTERVAL 6 DAY), '%Y-%m-%d 23:59:59') AS '本周末';
  1. 上周(自然周)
	SELECT DATE_FORMAT( DATE_SUB( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), INTERVAL 1 WEEK), '%Y-%m-%d 00:00:00') AS '上周一';
	SELECT DATE_FORMAT( SUBDATE(CURDATE(), WEEKDAY(CURDATE()) + 1), '%Y-%m-%d 23:59:59') AS '上周末';
  1. 本月(自然月)
	SELECT DATE_FORMAT( CURDATE(), '%Y-%m-01 00:00:00') AS '本月初';
	SELECT DATE_FORMAT( LAST_DAY(CURDATE()), '%Y-%m-%d 23:59:59') AS '本月末';
  1. 上月(自然月)
	SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01 00:00:00') AS '上月初';
	SELECT DATE_FORMAT( LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)), '%Y-%m-%d 23:59:59') AS '上月末';
  1. 本年(自然年)
	SELECT DATE_FORMAT( CURDATE(), '%Y-01-01 00:00:00') AS '本年初';
	SELECT CONCAT(YEAR(CURDATE()),'-12-31 23:59:59') AS '本年末';
  1. 去年(自然年)
	SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 YEAR), '%Y-01-01 00:00:00') AS '去年初';
	SELECT DATE_FORMAT(DATE_SUB(curdate(), INTERVAL DAYOFYEAR(curdate()) DAY), '%Y-%m-%d 23:59:59') AS '去年末';

你可能感兴趣的:(MySQL花式查询集合)