I’m very new to MySQL triggers and procedure code in general, so this is probably a stupid mistake on my part. Here’s the code that is not working.
CREATE TRIGGER add_to_cart
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
SET @status = ‘S’;
SET @user = NEW.user_id;
SET @order = NEW.order_id;
SELECT count(*) INTO @shipped_count
FROM orders
**WHERE (user_id = @user) AND (order_status = @status);**
SELECT MAX(priority) INTO @max FROM cart;
IF @max = 'NULL' THEN
SET @max = 0;
END IF;
SET @priority = @max + 1;
INSERT INTO cart VALUES (@user, @order, @priority);
**INSERT INTO log VALUES (@count, @user);**
END $$
The trigger works as expected until I include the WHERE clause on the SELECT count(*) INTO shipped_count. I can count all the records in the orders table, but when I try to specify certain types of records it does not work.
Also, the bold “INSERT INTO log” also fails for some reason, whereas the exact same syntax works for the INSERT directly above it.
Help! Thanks. I’m clueless.
Try replacing:
With:
Notice the regular single quotes instead of the rounded single quotes.