can someone please help me with this sql query. it gives me a 1064 error suggesting I have a syntax error in my query. I’ve looked for the error over 2 hours now and getting frustrated now.
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
thanks here is the error:
Error Code: 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 ‘?, INTERVAL 1 MONTH)) AND (date< ?), earnings, 0)) as Rev0
FROM
hat_adsens’ at line 2
You have
parameter place holderson your query, which do not work on pure sql. The only time?works is when you create aDynamic SQL.If you are working with query that is not a
Dynamic SQL, you should provide a value for that.On example to remove the syntax error on the statement is to create a
user variable, eg