1

phpMyAdmin tells me that the following code snippet is syntactically incorrect :

 SET @time := NOW();
 SET @timeset :=0;


 IF @timeset=1 
    THEN SET @time := DATE_ADD(@time,INTERVAL 10 SECOND);
    ELSE SET @time := DATE_SUB(NOW(),INTERVAL 1 DAY);
         SET @timeset := 1; 
    END IF; 

but I fail to see what's wrong with it.

The exact error message goes : enter image description here

UPDATE : thanks to wolfgangalther's answer, I found the following workaround :

SET @time := NOW();
SET @timeset :=0;
SET @time=IF(@timeset=1,DATE_ADD(@time,INTERVAL 10 SECOND),DATE_SUB(NOW(),INTERVAL 1 DAY)); 
SET @timeset:=1; 
Ewan Delanoy
  • 1,276
  • 2
  • 13
  • 31
  • Are `timeset` and `time` declared? – hjpotter92 Nov 03 '14 at 08:43
  • @hjpotter92 I already thought of that. So yes, they are declared ; I just enlarged my code snippet to show that – Ewan Delanoy Nov 03 '14 at 08:49
  • As per your most recent question, we prefer code and error messages to be supplied as text. The code is fine here, but the error is a screenshot. Just copy it from your browser to the clipboard, and post it into your question. – halfer Dec 29 '14 at 13:21

2 Answers2

3

From the MySQL manual:

MySQL supports the IF, CASE, ITERATE, LEAVE LOOP, WHILE, and REPEAT constructs for flow control within stored programs.

You are not allowed to issue an IF construct on its own!

There is also a short discussion about it here (including some alternatives): https://dba.stackexchange.com/questions/80132/using-if-statement-outside-of-a-stored-procedure-or-function

Note: Using an IF statement at that place has been requested as a feature in the past, but is not implemented into MySQL as of now.

Edit:
Stored programs are either stored procedures or stored functions. Stored functions are the same thing as stored procedures, they just return a value.

Read more about stored procedures:

Community
  • 1
  • 1
wolfgangwalther
  • 1,226
  • 7
  • 15
0

This kind of IF ELSE you can only use in stored procedures, But you can use If condition in query as given below.

IF(expr,if_true_expr,if_false_expr)


Example

SELECT customerNumber,
       customerName,
       IF(state IS NULL,'N/A',state) state,
       country
FROM customers;
mahesh
  • 1,311
  • 1
  • 13
  • 26