我使用觸發器來插入一行,並希望使用最後創建的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