如何获得去年的日期范围?

时间:2014-03-08 22:09:57

标签: mysql sql

我希望LastYear/1/1LastYear/12/31

之间的日期范围

我知道我可以做到这一点

date_sub(now(), interval 1 year)。但这会让我2013/03/08。不确定如何更改日期和月份。

SELECT *
FROM orders
WHERE dispatch_date between  `LastYear/1/1` AND `LastYear/12/31`

2 个答案:

答案 0 :(得分:1)

您可以轻松创建所需日期:

SELECT *
FROM orders
WHERE dispatch_date >= MAKEDATE(YEAR(NOW()) - 1, 1) -- first day of previous year
 AND dispatch_date < MAKEDATE(YEAR(NOW()), 1)  -- first day of current year

答案 1 :(得分:0)

我建议您使用YEAR()

SET @LastYear = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR));
SELECT *
FROM orders
WHERE dispatch_date 
    BETWEEN CONCAT(@LastYear,'-01-01') AND CONCAT(@LastYear,'-12-31')