SELECT
NOW() 当前日期,
DATE_ADD(NOW(), INTERVAL - 400 DAY) 历史日期,
TIMESTAMPDIFF(
YEAR,
DATE_ADD(NOW(), INTERVAL - 400 DAY),
NOW()
) AS 相差年;
SELECT
NOW() 当前日期,
DATE_ADD(NOW(), INTERVAL - 400 DAY) 历史日期,
TIMESTAMPDIFF(
MONTH,
DATE_ADD(NOW(), INTERVAL - 400 DAY),
NOW()
) AS 相差月;
SELECT
NOW() 当前日期,
DATE_ADD(NOW(), INTERVAL - 400 DAY) 历史日期,
TIMESTAMPDIFF(
WEEK,
DATE_ADD(NOW(), INTERVAL - 400 DAY),
NOW()
) AS 相差周;
整理了一下三种方式:
SELECT
NOW() 当前日期,
DATE_ADD(NOW(), INTERVAL - 400 DAY) 历史日期,
TIMESTAMPDIFF(
DAY,
DATE_ADD(NOW(), INTERVAL - 400 DAY),
NOW()
) AS 相差日;
SELECT
TO_DAYS('2018-09-10') - TO_DAYS('2017-09-01') AS 相差天数;
SELECT
DATEDIFF(NOW(), '2018-09-01') AS 相差天数;