0

so I'm try to create auto increment by using trigger with case the case works when the parameter insert but the value is not auto increment and stay "001" here is the code

DELIMITER $$

USE `onlinestore`$$

DROP TRIGGER /*!50032 IF EXISTS */ `trigger_sensors`$$

CREATE
    /*!50017 DEFINER = 'root'@'localhost' */
    TRIGGER `trigger_sensors` BEFORE INSERT ON `sensors` 
    FOR EACH ROW 
BEGIN
  IF (NEW.label IS NULL) THEN
    -- Find max existed label for specified sensor type
    SELECT
      MAX(label) INTO @max_label
    FROM
      sensors
    WHERE
      TYPE = NEW.name;

    IF (@max_label IS NULL) THEN
      SET @label =
        CASE NEW.name
        WHEN 'VIP' THEN 'VIP'
        WHEN 'REGULER' THEN 'R'
        WHEN 'MROOM' THEN 'MR'
        ELSE 'UNKNOWN'
      END;

      -- Set first sensor label
      SET NEW.label = CONCAT(@label, '001');
    ELSE
      -- Set next sensor label
      SET NEW.label = CONCAT(SUBSTR(@max_label, 1, 2), LPAD(SUBSTR(@max_label, 3) + 1, 4, '0'));
    END IF;
  END IF;
END;
$$

DELIMITER ;

here is the value in database enter image description here

i cannot find where did I does wrong.... can someone help me?

1 Answer 1

1

In the first SELECT query you have a mistake in the WHERE clause. It should be WHERE NAME = NEW.name;

Sign up to request clarification or add additional context in comments.

1 Comment

thnks, i wasn't pay attention at that

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.