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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2263453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:17:40  来源:igfitidea点击:

Get the latest inserted id in a trigger?

mysqldatabasetriggers

提问by ajsie

I use a trigger to insert a row and want to use the last created id for using in the subsequent query.

我使用触发器插入一行,并希望使用上次创建的 id 用于后续查询。

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

回答by Mitch Wheat

Have you looked at LAST_INSERT_ID()? But be aware:

你看过LAST_INSERT_ID()吗?但请注意:

If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only.

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

回答by AtlantaKid

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

我想使用像“AAA”这样的公司首字母缩写并将插入 ID 添加到其中并将其用作内部公司 ID,这就是我获取最后一个当前插入 ID 的方式

DELIMITER //
CREATE TRIGGER company_run_before_insert BEFORE INSERT ON ap_company 
FOR EACH ROW
BEGIN

SET @lastID = (SELECT id FROM ap_company ORDER BY id DESC LIMIT 1);
IF @lastID IS NULL OR @lastID = '' THEN
    SET @lastID = 0;
END IF;
SET @lastID = @lastID +1;
SET NEW.ap_company_id = concat(NEW.company_initials,'-', @lastID);
END;

回答by Darkhan ZD

USE NEW.id

使用新的.id

BEGIN
insert INTO test_questions (test_id, question, variant1, variant2, variant3, w1, type_id) 
SELECT NEW.id, t1.question, t1.v1, t1.v2, t1.v3, t1.answer, 1
FROM new_minitest_questions t1
WHERE t1.mt_id = NEW.old_id;
END

回答by user3785966

Following trigger will get the last Auto Increment value from the Information Schema. I had written this trigger to generate a slug.

以下触发器将从信息架构中获取最后一个自动增量值。我写了这个触发器来生成一个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.

例如,如果列的名称是“Name”并且列具有诸如“Robin Shankar”之类的值,则触发器将用“-”替换空格,并将自动增量 ID 附加到 slug 的末尾,从而形成唯一的蛞蝓。

robin_shankar_9071

robin_shankar_9071

DELIMITER //
CREATE TRIGGER insert_slug_sd
BEFORE INSERT ON `your_table_name`
FOR EACH ROW
BEGIN

DECLARE auto_increment_ INT(11);

SELECT 
    `auto_increment` 
INTO 
    auto_increment_
FROM INFORMATION_SCHEMA.TABLES
    WHERE 
table_name = 'your_table_name';

SET NEW.`Slug` = CONCAT(LOWER(REPLACE(NEW.`Name`,' ','-')),'-',auto_increment_);
END; //

DELIMITER;

回答by Антон Баранов

SET NEW.num=CONCAT("А-", (
  SELECT `auto_increment` 
  FROM INFORMATION_SCHEMA.TABLES    
  WHERE table_name = 'menu')
)