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

发布于 2024-08-21 07:03:32 字数 380 浏览 5 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

断念 2024-08-28 07:03:32

您是否看过 LAST_INSERT_ID()< /a>?但请注意:

如果您使用
单个 INSERT 语句,
LAST_INSERT_ID() 返回值
为第一个插入行生成
仅此而已。

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

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

暗地喜欢 2024-08-28 07:03:32

我想使用公司首字母缩写(如“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;

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

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;
悲喜皆因你 2024-08-28 07:03:32

您可以从服务器传递插入行的索引(循环索引)
在触发器“插入之前”中,您可以执行以下操作:

BEGIN
if (NEW.id=0) then
set @A = (SELECT AUTO_INCREMENT
     FROM information_schema.TABLES
     WHERE TABLE_SCHEMA = "schema"
    AND TABLE_NAME = "table");
    end if;
SET NEW.id  = NEW.id + @A ;
END

假设 LAST_INSERT_ID() = 5

并且您尝试在同一插入语句中添加 3 行...

因此,第一行将有 5 + 0

秒: 5 + 1

第三次 : 5 + 2

you can pass the index of the inserted row from the server (the loop index)
and in the trigger "before insert" you can do the following :

BEGIN
if (NEW.id=0) then
set @A = (SELECT AUTO_INCREMENT
     FROM information_schema.TABLES
     WHERE TABLE_SCHEMA = "schema"
    AND TABLE_NAME = "table");
    end if;
SET NEW.id  = NEW.id + @A ;
END

let we assume that LAST_INSERT_ID() = 5

and you are trying to add 3 rows at the same insert statement ...

so , the first row would have 5 + 0

second : 5 + 1

third : 5 + 2

秋千易 2024-08-28 07:03:32

使用新的.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

USE NEW.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
猫七 2024-08-28 07:03:32

以下触发器将从信息架构中获取最后的自动增量值。我编写了这个触发器来生成一个 slug。

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

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;

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

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;
疾风者 2024-08-28 07:03:32
SET NEW.num=CONCAT("А-", (
  SELECT `auto_increment` 
  FROM INFORMATION_SCHEMA.TABLES    
  WHERE table_name = 'menu')
)
SET NEW.num=CONCAT("А-", (
  SELECT `auto_increment` 
  FROM INFORMATION_SCHEMA.TABLES    
  WHERE table_name = 'menu')
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文