0

Trying to create a MySQL event that resets a soldout status (indicated as 1).

The logic I'm trying to create is a simple IF statement.

If soldout = 1 AND Quantity > 0 in the same row

UPDATE soldout to 0

Here is my event

CREATE EVENT soldoutreset
ON SCHEDULE EVERY 5 MINUTE
DO
IF db.Inventory.soldout = 1
AND db.Inventory.Quantity > 0 THEN
UPDATE db.Inventory SET soldout = 0
WHERE soldout = 1 AND Quantity > 1
END IF;

This is resulting in an error, 'END IF' at line 9

#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 'END IF' at line 9

Can anyone tell why this is not working?

1 Answer 1

6

This should work, you are missing a part of the syntax:

DELIMITER |

CREATE EVENT soldoutreset
ON SCHEDULE EVERY 5 MINUTE
    DO
    BEGIN
        IF db.Inventory.soldout = 1 AND db.Inventory.Quantity > 0 THEN
            UPDATE db.Inventory SET soldout = 0
            WHERE soldout = 1 AND Quantity > 1;
        END IF;
    END |

DELIMITER ;

Update:

If the event is not running as expected, it might be because the event scheduler is off. It can be enabled with:

SET GLOBAL event_scheduler = ON; 
Sign up to request clarification or add additional context in comments.

9 Comments

I am still getting the same (or similar error) ...to use near 'END IF; END' at line 8
In my initial answer I missed BEGIN dans I updated the answer since. Try again with the current version please
I believe I had the BEGIN inititally, I don't see an edit. Still, I try and still getting the END IF; END error.
@BrianBruman I was missing the ; at the end of the update query, please try again.
Yes it processed correctly. Thank you for the help with the missing syntax. Still looking into why it is not working properly, I'm trying different things. After 5 minutes my test row with a Quantity of 17 and a soldout of 1 still shows 1 after 5 minutes which is not my intended 0
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.