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;
The ERROR 1064 (42000) mainly occurs when the syntax isn't set correctly i.e. error in applying the backtick symbol or while creating a database without them can also create an error, if you will use hyphen in the name, for example, Demo-Table will result in ERROR 1064 (42000). Now database is created successfully.
The IS NULL constraint can be used whenever the column is empty and the symbol ( ' ') is used when there is empty value. mysql> SELECT * FROM ColumnValueNullDemo WHERE ColumnName IS NULL OR ColumnName = ' '; After executing the above query, the output obtained is.
To look for NULL values, you must use the IS NULL test. The following statements show how to find the NULL phone number and the empty phone number: mysql> SELECT * FROM my_table WHERE phone IS NULL; mysql> SELECT * FROM my_table WHERE phone = ''; See Section 3.3.
Lost connection to MySQL server If an error message like “Lost connection to MySQL server” appears while querying the database, it is certain that the error has occurred because of network connection issues.
If by any chance the @sql
variable still holds NULL
value after the first select statement then you are going to encounter an exception later on while executing the prepare statement.
Look at the following select statement using CONCAT
SET @sql := NULL; SELECT CONCAT('abc',@sql,'def');
The result is NULL
. Although you might expect the result to be abcdef
.
In order to get abcdef
you need to do this
SET @sql := NULL; SELECT CONCAT('abc',IFNULL(@sql,''),'def');
You may try any of the following if it resolves the issue:
Either
1) SET @sql := '';
OR
2) If you want to keep this line SET @sql = NULL;
then change the portion of the final query like this SET @sql = CONCAT("SELECT failure_code ", IF(@sql IS NULL, '',CONCAT(',',@sql)),
Here's the final query:
SET @sql = CONCAT("SELECT
failure_code ", IF(@sql IS NULL, '',CONCAT(',',@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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With