小编典典

获取触发器中最新插入的ID?

mysql

我使用触发器来插入行,并希望使用上一个创建的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

阅读 525

收藏
2020-05-17

共1个答案

小编典典

您是否看过LAST_INSERT_ID()?但要注意:

如果使用单个INSERT语句插入多行,则LAST_INSERT_ID()仅返回为第一个插入行生成的值。

2020-05-17