MySQL 1064语法错误无法找到错误

时间:2013-01-21 14:12:36

标签: mysql sql

有人可以帮我这个SQL查询。它给了我一个1064错误,表明我的查询中有语法错误。我现在已经看了2个多小时的错误,现在感到很沮丧。

SELECT 
 SUM(IF( (`date` >= DATE_SUB(?, INTERVAL 6 MONTH)) AND (`date` < DATE_SUB(?, INTERVAL 5 MONTH)), earnings, 0)) AS Rev5,
 SUM(IF( (`date` >= DATE_SUB(?, INTERVAL 5 MONTH)) AND (`date` < DATE_SUB(?, INTERVAL 4 MONTH)), earnings, 0)) AS Rev4,
 SUM(IF( (`date` >= DATE_SUB(?, INTERVAL 4 MONTH)) AND (`date` < DATE_SUB(?, INTERVAL 3 MONTH)), earnings, 0)) AS Rev3,
 SUM(IF( (`date` >= DATE_SUB(?, INTERVAL 3 MONTH)) AND (`date` < DATE_SUB(?, INTERVAL 2 MONTH)), earnings, 0)) AS Rev2,
 SUM(IF( (`date` >= DATE_SUB(?, INTERVAL 2 MONTH)) AND (`date` < DATE_SUB(?, INTERVAL 1 MONTH)), earnings, 0)) AS Rev1,
 SUM(IF( (`date` >= DATE_SUB(?, INTERVAL 1 MONTH)) AND (`date` < ?), earnings, 0)) AS Rev0
FROM
 hat_adsense_stats
GROUP BY 
 domain
ORDER BY
 domain

谢谢,这是错误:

  

错误代码:1064   您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'?,INTERVAL 1 MONTH)和(date&lt;?),收益,0))附近使用正确的语法作为Rev0   从    第2行的hat_adsens

1 个答案:

答案 0 :(得分:2)

您的查询有parameter place holders,这对纯sql不起作用。 ?唯一有效的时间是您创建Dynamic SQL

如果您使用的查询不是Dynamic SQL,则应为其提供值。

在示例中删除语句上的语法错误是创建user variable,例如

SET @date = CURDATE();
SELECT 
 SUM(IF( (`date` >= DATE_SUB(@date, INTERVAL 6 MONTH)) AND (`date` < DATE_SUB(@date, INTERVAL 5 MONTH)), earnings, 0)) AS Rev5,
 SUM(IF( (`date` >= DATE_SUB(@date, INTERVAL 5 MONTH)) ........