SCS SCS - 4 months ago 330
SQL Question

MYSQL: IF statement is not valid in this position?

I have checked/comapred mysql statement serveral times from google but it is still now working :(. I am using workbench 6.3, it gives an 1064 error or


syntax error : 'IF' is not a valid input at this position.


IF(EXISTS(SELECT * FROM RECORDS WHERE FORMID = 200002016 AND TimeUploaded > '2016-07-17 03:13:39' AND TimeUploaded < '2016-07-17 03:13:39'))
Begin
SELECT TimeUploaded from RECORDS
End;

Answer

You cannot use IF in an SQL statement. You can only use it in a stored procedure or a function.

You can rewrite your statement like this:

SELECT TimeUploaded from RECORDS
WHERE FORMID = 200002016
  AND TimeUploaded > '2016-07-17 03:13:39'
  AND TimeUploaded < '2016-07-17 03:13:39'