I got this error when tried to execute this:
#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 'NULL' at line 1
Can't seems to find what is the problem. Appreciate if anyone can help
SET @sql = NULL;
SELECT
GROUP_CONCAT(
DISTINCT CONCAT (
"SUM(IF(DATE(FROM_UNIXTIME(machine_stop)) = '",
DATE(FROM_UNIXTIME(machine_stop)),"' ,
(machine_start-machine_stop)/3600, 0)) AS ",
DATE(FROM_UNIXTIME(machine_stop))
)
) INTO @sql
FROM
downtime_data
WHERE
DATE(FROM_UNIXTIME(machine_stop)) >= DATE(NOW()) - INTERVAL 7 DAY;
SET @sql = CONCAT("SELECT
failure_code, ", @sql, "
FROM
downtime_data
WHERE
p.machine='HH1' AND
DATE(FROM_UNIXTIME(machine_stop)) >= DATE(NOW()) - INTERVAL 7 DAY
GROUP BY
failure_code,
DATE(FROM_UNIXTIME(machine_stop))"
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
@sql
variable after the first select statement. There's a possibility if the@sql
variable is stillNULL
after the first select statement then you will get an exception while executingstmt
. – ZhangSET @sql = CONCAT("SELECT failure_code, ", IFNULL(@sql,''), ...."
– ZhangSET @SQL = NULL
entirely. There's no path through the rest of the code where @SQL will not be assigned a new value. If you must initialize it to clear old content, simply set it to an empty string. – LanderSELECT @`sql`;
. – Leonidaleonidas