I use a trigger to insert a row and want to use the last created id for using in the subsequent query.
How could I do this?
The code looks like:
BEGIN
IF (NEW.counter >= 100) THEN
INSERT INTO tagCategories (name, counter) VALUES ('unnamed', NEW.counter);
// here i want to have access to the above inserted id
UPDATE tagCategories2tagPairs SET tagCategoryId = <<ID_HERE>> WHERE tagPairId = OLD.id
END IF;
END
I wanted to use a company initials like "AAA" and add the insert ID to it and use it as a internal company ID and this is how I picked up the last current insert ID
Following trigger will get the last Auto Increment value from the Information Schema. I had written this trigger to generate a slug.
For instances, if the column's name is "Name" and column has a value such as "Robin Shankar", the trigger would replace spaces with "-" and also append the Auto Increment Id to the end of the slug, hence making a unique slug.
robin_shankar_9071
Have you looked at LAST_INSERT_ID()? But be aware:
USE NEW.id