- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
CREATE FUNCTION `getSequenceNumber` (
company_id INTEGER, sequence_name varchar(255)) RETURNS INT(10)
BEGIN
INSERT INTO sequences (`company_id`, `name`, `value`)
VALUES (company_id, sequence_name, LAST_INSERT_ID(1))
ON DUPLICATE KEY UPDATE
`value` = LAST_INSERT_ID(value + 1);
RETURN LAST_INSERT_ID(); END
CREATE TABLE `sequences` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`company_id` int(10) unsigned NOT NULL,
`value` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `sequences_name_company_id_unique` (`name`,`company_id`),
KEY `sequences_company_id_index` (`company_id`),
KEY `sequences_value_index` (`value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
//sample output
MariaDB [testdb]> select version();
+----------------+
| version() |
+----------------+
| 10.2.6-MariaDB |
+----------------+
1 row in set (0.00 sec)
MariaDB [testdb]> select `getSequenceNumber`(1,'sequence_021');
+---------------------------------------+
| `getSequenceNumber`(1,'sequence_021') |
+---------------------------------------+
| 2 |
+---------------------------------------+
1 row in set (0.03 sec)
MariaDB [testdb]> select `getSequenceNumber`(1,'sequence_0212');
+----------------------------------------+
| `getSequenceNumber`(1,'sequence_0212') |
+----------------------------------------+
| 5 |
+----------------------------------------+
1 row in set (0.03 sec)
MariaDB [testdb]> select `getSequenceNumber`(1,'new_sequence123');
+------------------------------------------+
| `getSequenceNumber`(1,'new_sequence123') |
+------------------------------------------+
| 6 |
+------------------------------------------+
1 row in set (0.03 sec)
我的 MariaDB 有这个函数,它可以工作,但问题是插入时,它插入的新 ID 是 <last_id>
+ <last value>
有没有办法清除/刷新 LAST_INSERT_ID
在插入记录之前?
编辑:添加了创建sql语句和示例输出
最佳答案
我无法重现该问题:
MariaDB [_]> SELECT VERSION();
+----------------+
| VERSION() |
+----------------+
| 10.2.6-MariaDB |
+----------------+
1 row in set (0.00 sec)
MariaDB [_]> DROP TABLE IF EXISTS `sequences`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaMariaDB [_]> CREATE TABLE IF NOT EXISTS `sequences` (
-> `name` VARCHAR(255) PRIMARY KEY,
-> `value` BIGINT UNSIGNED NOT NULL
-> );
Query OK, 0 rows affected (0.01 sec)
MariaDB [_]> DROP FUNCTION IF EXISTS `getSequenceNumber`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [_]> DELIMITER //
MariaDB [_]> CREATE OR REPLACE FUNCTION `getSequenceNumber` (
-> `sequence_name` VARCHAR(255)
-> )
-> RETURNS BIGINT UNSIGNED
-> BEGIN
-> INSERT INTO `sequences` (`name`, `value`)
-> VALUES (`sequence_name`, LAST_INSERT_ID(1))
-> ON DUPLICATE KEY UPDATE `value` = LAST_INSERT_ID(`value` + 1);
-> RETURN LAST_INSERT_ID();
-> END//
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER ;
MariaDB [_]> SELECT `getSequenceNumber`('sequence_0'); -- 1
+-----------------------------------+
| `getSequenceNumber`('sequence_0') |
+-----------------------------------+
| 1 |
+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_0'); -- 2
+-----------------------------------+
| `getSequenceNumber`('sequence_0') |
+-----------------------------------+
| 2 |
+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_1'); -- 1
+-----------------------------------+
| `getSequenceNumber`('sequence_1') |
+-----------------------------------+
| 1 |
+-----------------------------------+
1 row in set (0.01 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_2'); -- 1
+-----------------------------------+
| `getSequenceNumber`('sequence_2') |
+-----------------------------------+
| 1 |
+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_0'); -- 3
+-----------------------------------+
| `getSequenceNumber`('sequence_0') |
+-----------------------------------+
| 3 |
+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_2'); -- 2
+-----------------------------------+
| `getSequenceNumber`('sequence_2') |
+-----------------------------------+
| 2 |
+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_1'); -- 2
+-----------------------------------+
| `getSequenceNumber`('sequence_1') |
+-----------------------------------+
| 2 |
+-----------------------------------+
1 row in set (0.00 sec)
更新
MariaDB [_]> SELECT VERSION();
+----------------+
| VERSION() |
+----------------+
| 10.2.6-MariaDB |
+----------------+
1 row in set (0.00 sec)
MariaDB [_]> DROP FUNCTION IF EXISTS `getSequenceNumber`;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DROP TABLE IF EXISTS `sequences`;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS `sequences` (
-> `name` VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL,
-> `company_id` BIGINT UNSIGNED unsigned NOT NULL,
-> `value` BIGINT UNSIGNED NOT NULL,
-> PRIMARY KEY `sequences_name_company_id_unique` (`name`, `company_id`),
-> KEY `sequences_company_id_index` (`company_id`),
-> KEY `sequences_value_index` (`value`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER //
MariaDB [_]> CREATE OR REPLACE FUNCTION `getSequenceNumber` (
-> `_company_id` BIGINT UNSIGNED,
-> `sequence_name` VARCHAR(255)
-> ) RETURNS BIGINT UNSIGNED
-> BEGIN
-> INSERT INTO `sequences` (`name`, `company_id`, `value`)
-> VALUES (`sequence_name`, `_company_id`, LAST_INSERT_ID(1))
-> ON DUPLICATE KEY UPDATE `value` = LAST_INSERT_ID(`value` + 1);
-> RETURN LAST_INSERT_ID();
-> END//
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER ;
MariaDB [_]> SELECT `getSequenceNumber`(1, 'sequence_021'); -- 1
+----------------------------------------+
| `getSequenceNumber`(1, 'sequence_021') |
+----------------------------------------+
| 1 |
+----------------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`(2, 'sequence_021'); -- 1
+----------------------------------------+
| `getSequenceNumber`(2, 'sequence_021') |
+----------------------------------------+
| 1 |
+----------------------------------------+
1 row in set (0.01 sec)
MariaDB [_]> SELECT `getSequenceNumber`(1, 'sequence_021'); -- 2
+----------------------------------------+
| `getSequenceNumber`(1, 'sequence_021') |
+----------------------------------------+
| 2 |
+----------------------------------------+
1 row in set (0.02 sec)
MariaDB [_]> SELECT `getSequenceNumber`(2, 'sequence_021'); -- 2
+----------------------------------------+
| `getSequenceNumber`(2, 'sequence_021') |
+----------------------------------------+
| 2 |
+----------------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`(1, 'sequence_0212'); -- 1
+-----------------------------------------+
| `getSequenceNumber`(1, 'sequence_0212') |
+-----------------------------------------+
| 1 |
+-----------------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`(1, 'sequence_021'); -- 3
+----------------------------------------+
| `getSequenceNumber`(1, 'sequence_021') |
+----------------------------------------+
| 3 |
+----------------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`(1, 'new_sequence123'); -- 1
+-------------------------------------------+
| `getSequenceNumber`(1, 'new_sequence123') |
+-------------------------------------------+
| 1 |
+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`(1, 'new_sequence123'); -- 2
+-------------------------------------------+
| `getSequenceNumber`(1, 'new_sequence123') |
+-------------------------------------------+
| 2 |
+-------------------------------------------+
1 row in set (0.01 sec)
关于mysql - 有问题的 LAST_INSERT_ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44460643/
这个问题在这里已经有了答案: Easy mysql question regarding primary keys and an insert (4 个答案) 关闭 9 年前。 所以我想用公共(pu
我环顾四周,仍然对如何获取最后插入的 ID 感到困惑。我在正在执行的 mysql 语句末尾添加了语句 SELECT LAST_INSERT_ID();。我将值存储在 prID = Convert.To
所以我有如下 3 个表。 TOPICS TOPIC_TAGS Tags topic_id tag_id tag_id topic_data topic_id
$stmt2 = $db->prepare("INSERT INTO usertabbrige(`tabId`,`uId`)
我使用 PDO 在 PHP/MySql 上运行。 假设我有一个这样的查询。 $OrderId 在“Orders”表中自动递增。我可以使用 LAST_INSERT_ID() 在“Orders”和“Bou
CREATE FUNCTION `getSequenceNumber` ( company_id INTEGER, sequence_name varchar(255)) RETURNS INT(10
更新2:如果我手动将下面的EXECUTE代码示例替换为要执行的实际SQL代码,它工作得非常好,这告诉我这个问题已经厌倦了PREPARE、EXECUTE和DEALLOCATE PREPARE。 更新 1
我有以下代码。问题在于 SELECT LAST_INSERT_ID() 这总是返回最后插入的行。如果在获取最后一行之前实际插入了一行,我是否可以使用 INSERT IGNORE 检查,或者我是否可以简
我看不出我做错了什么,请帮忙。我正在学习 Select Last_Insert_Id,但我已经被困了几天了。 $result2 = $mysqli1->query("SELECT LAST_INSER
我有 3 个表: Words { wordId, name } WordGroups { wordGroupId, type} WordSets { wordSetId, wordId, wordGr
在 mysql 中创建线程安全序列时,我遇到了 mysql 文档 - https://dev.mysql.com/doc/refman/5.6/en/information-functions.htm
是否可以在插入查询中使用 LAST_INSERT_ID()? INSERT INTO mytable (col1, col2) VALUES ('val1', null), ('val2', LAST
我看过 official documentation但我还是有点困惑。 假设我有一个程序,它运行并执行插入,然后我请求 LAST_INSERT_ID(),我是从我的程序实例运行的插入中获取最后一个插入
我有一个包含三个查询的 mysql 事务 在第三个查询中,我试图从前两个查询中提取 last_insert_id 值。 VALUES ('".$result1[last_id]."','".$resu
我在 mysql 中有一个存储过程,它正在更新很多行然后插入一个(具体来说是分层数据结构)。除了我设置 @insert_id 变量的最后一段代码外,不要看太多代码。 CREATE DEFINER=`r
例如,如果我有插入查询: INSERT INTO user(username) VALUES('admin'); 然后用获取插入记录的id LAST_INSERT_ID(); 看起来找到了,但是如果在
我有两个表格,问题 和答案。answers 包含一个键 *question_id* 当我创建一个问题时,我将一条记录插入到questions 表中,并将几条记录插入到answers 表中。是否可以使用
我有这个问题:我有一个存储过程,它将行插入到具有自动递增列 a 的表 A,然后将一行插入到表 B,该表具有到列 A.a 的外键 b。我正在使用 LAST_INSERT_ID 获取列新插入行的值。但是
假设我们有 2 个数据库:a 和 b,以及表 a.test1 和 b.test2. 如果我需要在表a.test1中插入一行,并返回LAST_INSERT_ID()插入到b.test2中,将LAST_I
如果我有以下两个功能: int AccessDb::InsertColValue(string tableName, string col, string val) { try {
我是一名优秀的程序员,十分优秀!