我使用触发器来插入一行,并希望使用最后创建的ID在后续查询中使用。在触发器中获取最新插入的ID?
我该怎么做?
代码如下:
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
当我使用这个在after_trigger,它返回以前插入ID出于某种原因...例如'NEW.id'可以使用。并且例如'OLD.id'可以用于删除或更新(至少这是文档中写的)。 [docs。](https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html) – ravo10 2017-12-10 01:53:35