今天
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 year(now()) as c_year union all
select year(now())-1 as c_year union all
select year(now())-2 as c_year union all
select year(now())-3 as c_year union all
select year(now())-4 as c_year union all
select year(now())-5 as c_year union all
select year(now())-6 as c_year union all
select year(now())-7 as c_year union all
select year(now())-8 as c_year union all
select year(now())-9 as c_year
获取连续的月份
SELECT DATE_FORMAT(( CURDATE()), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 1 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 2 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 3 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 4 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 5 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 6 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 7 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 8 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 9 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 10 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT(( CURDATE() - INTERVAL 11 MONTH ), '%Y-%m' ) AS `month`
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;