Mysql DATE_SUB(NOW(),INTERVAL 365天)MYSQL错误

时间:2015-10-09 07:30:32

标签: jquery mysql database

SELECT DATE_FORMAT(created_date,'%W') as day, DATE_FORMAT(created_date,'%d') as dat, DATE_FORMAT(created_date,'%M') as mon, created_date as dated, SUM(price) AS amount FROM `order_invoice` BETWEEN DATE_SUB(NOW(), INTERVAL 365 DAY) AND  NOW()  GROUP BY YEAR(created_date) ORDER BY created_date DESC

我收到此错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BETWEEN DATE_SUB(NOW(), INTERVAL 365 DAY) AND  NOW()  GROUP BY YEAR(created_date' at line 1

2 个答案:

答案 0 :(得分:1)

你确实在where子句中提到了列名。在BETWEEN之前提及列名的WHERE 像

SELECT 
 DATE_FORMAT(created_date,'%W') as day, 
 DATE_FORMAT(created_date,'%d') as dat, 
 DATE_FORMAT(created_date,'%M') as mon,
 created_date as dated, SUM(price) AS amount
FROM `order_invoice` 
WHERE created_date BETWEEN DATE_SUB(NOW(), INTERVAL 365 DAY) AND  NOW()
GROUP BY YEAR(created_date) 
ORDER BY created_date DESC

答案 1 :(得分:0)

如果created_date是一个日期字段(没有时间信息),我会使用它:

SELECT ... FROM order_invoice
WHERE created_date >= current_date() - INTERVAL 365 DAY
      AND created_date <= current_date()

(或者您可能希望使用>代替>=

但是,我无法完全理解您要从查询中返回的内容,因为您是按年份分组(created_date)并选择非聚合列,例如created_date。