mysql 实用的sql
计算今日:
SELECT CURRENT_DATE();
计算本周一日期:
SELECT DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY);
计算本周日日期:
SELECT DATE_ADD(CURDATE(), INTERVAL 6 - WEEKDAY(CURDATE()) DAY);
计算本月第一天日期:
SELECT DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE())-1 DAY);
计算本月最后一天日期:
SELECT DATE_SUB(DATE_ADD(DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH), INTERVAL 1 DAY);
计算下月第一天日期:
SELECT DATE_ADD(DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH);
计算下月最后一天日期:
SELECT DATE_SUB(DATE_ADD(DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 2 MONTH), INTERVAL 1 DAY);
计算本季度第一天日期:
SELECT DATE_SUB(DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE())-1 DAY), INTERVAL CASE WHEN MOD(MONTH(CURDATE()), 3) = 1 THEN 0 WHEN MOD(MONTH(CURDATE()), 3) = 2 THEN 1 WHEN MOD(MONTH(CURDATE()), 3) = 0 THEN 2 END MONTH);
计算本季度最后一天日期:
SELECT DATE_SUB(DATE_ADD(DATE_SUB(DATE_SUB(CURDATE(), INTERVAL DAY(CURDATE())-1 DAY), INTERVAL CASE WHEN MOD(MONTH(CURDATE()), 3) = 1 THEN 0 WHEN MOD(MONTH(CURDATE()), 3) = 2 THEN 1 WHEN MOD(MONTH(CURDATE()), 3) = 0 THEN 2 END MONTH), INTERVAL 3 MONTH), INTERVAL 1 DAY);