原理:10为被比较数,后面1,3,5,7为比较数,将后面四个依次与10比较,看后面数字组有多少个少于10,则返回其个数。前提是后面数字组为从小到大排列,否则返回结果0。
如:interval 1 day ,解释为将时间间隔设置为1天。
select date_add(curdate(), interval - day(curdate()) + 1 day);
今天是当月的第几天:SELECT DAYOFMONTH( NOW());
select last_day(curdate());
select date_add(curdate()-day(curdate())+1,interval -1 month);
select last_day(date_sub(now(),interval 1 month));
select date_add(curdate()-day(curdate())+1,interval 1 month);
select last_day(date_sub(now(),interval -1 month));
select day(last_day(curdate()));
select date_sub(curdate(), interval 1 month);
select unix_timestamp(date_sub(now(),interval 1 month));
select datediff(curdate(), date_sub(curdate(), interval 1 month));
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00') AS '今天开始';
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 23:59:59') AS '今天结束';
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 '昨天结束';
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 '上周末';
SELECT DATE_FORMAT( SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-1), '%Y-%m-%d 00:00:00') AS '本周一';
SELECT DATE_FORMAT( SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-7), '%Y-%m-%d 23:59:59') AS '本周末';
-- 上面的本周算法会有问题,因为mysql是按照周日为一周第一天,如果当前是周日的话,会把时间定为到下一周.
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 '本周末';
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 '上月末';
SELECT DATE_FORMAT( CURDATE(), '%Y-%m-01 00:00:00') AS '本月初';
SELECT DATE_FORMAT( LAST_DAY(CURDATE()), '%Y-%m-%d 23:59:59') AS '本月末';
SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY)
或
concat(year(now()),'-01-01')//当前年份的第一天
concat(year(now()),'-12-31')//当前年份的最后一天
SELECT
TIME_TO_SEC(TIMEDIFF('2018-09-30 19:38:45', '2018-08-23 10:13:01')) AS DIFF_SECOND1, -- 秒
UNIX_TIMESTAMP('2018-09-30 19:38:45')-UNIX_TIMESTAMP('2018-08-23 10:13:01') AS DIFF_SECOND2, -- 秒
TIMESTAMPDIFF(SECOND,'2018-08-23 10:13:01','2018-09-30 19:38:45') AS DIFF_SECOND3, -- 秒
TIMESTAMPDIFF(MINUTE,'2018-08-23 10:13:01','2018-09-30 19:38:45') AS DIFF_MINUTE, -- 分
TIMESTAMPDIFF(HOUR,'2018-08-23 10:13:01','2018-09-30 19:38:45') AS DIFF_HOUR, -- 小时
TIMESTAMPDIFF(DAY ,'2018-08-23 10:13:01','2018-09-30 19:38:45') AS DIFF_DATE1, -- 天
DATEDIFF('2018-09-30 19:38:45','2018-08-23 10:13:01') AS DIFF_DATE2, -- 天
TIMESTAMPDIFF(MONTH,'2018-08-23 10:13:01','2018-09-25 19:38:45') AS DIFF_MONTH, -- 月
TIMESTAMPDIFF(YEAR,'2018-08-23 10:13:01','2020-07-25 19:38:45') AS DIFF_YEAR -- 年
FROM DUAL;
select date_add(日期, interval 1 day); 日期加天
select date_add(日期, interval 1 hour); 日期加小时
select date_add(日期, interval 1 minute); 日期加分
select date_add(日期, interval 1 second);日期加秒
select date_add(日期, interval 1 microsecond); 日期加微秒
select date_add(日期, interval 1 week); 日期加周
select date_add(日期, interval 1 month); 日期加月
select date_add(日期, interval 1 quarter); 日期加季度
select date_add(日期, interval 1 year); 日期加年
SELECT ADDDATE(y.first, x.d - 1) as d
FROM (
SELECT 1 AS d UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12 UNION ALL
SELECT 13 UNION ALL
SELECT 14 UNION ALL
SELECT 15 UNION ALL
SELECT 16 UNION ALL
SELECT 17 UNION ALL
SELECT 18 UNION ALL
SELECT 19 UNION ALL
SELECT 20 UNION ALL
SELECT 21 UNION ALL
SELECT 22 UNION ALL
SELECT 23 UNION ALL
SELECT 24 UNION ALL
SELECT 25 UNION ALL
SELECT 26 UNION ALL
SELECT 27 UNION ALL
SELECT 28 UNION ALL
SELECT 29 UNION ALL
SELECT 30 UNION ALL
SELECT 31
) x,
(
SELECT '2019-09-05' - INTERVAL DAY('2019-09-05') - 1 DAY AS first, DAY(LAST_DAY('2019-09-05')) AS last
) y
WHERE x.d <= y.last
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-01-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-01-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-02-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-02-28'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-03-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-03-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-04-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-04-30'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-05-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-05-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-06-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-06-30'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-07-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-07-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-08-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-08-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-09-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-09-30'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-10-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-10-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-11-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-11-30'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-12-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-12-31'),'%Y-%m-%d') END_V
#当年第一天:
SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY);
#当年最后一天:
SELECT concat(YEAR(now()),'-12-31');
#当前week的第一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 1 DAY);
#当前week的最后一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 5 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);
#当前month的第一天:
SELECT concat(date_format(LAST_DAY(now()),'%Y-%m-'),'01');
#当前month的最后一天:
SELECT 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);