我希望LastYear/1/1
到LastYear/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`
答案 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')