gpt4 book ai didi

插入选择时Mysql自动增量跳转

转载 作者:IT老高 更新时间:2023-10-29 00:08:35 25 4
gpt4 key购买 nike

我正在测试插入-选择查询并注意到一个奇怪的结果。

CREATE TABLE `test` (
`cnt` int(11) NOT NULL AUTO_INCREMENT,
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
PRIMARY KEY (`cnt`)
)

CREATE TABLE `test_current` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL
)

首先我创建了两个表,并向 test_current 中插入了一些值

mysql> insert into test_current (a,b) values (1,1),(2,2);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

我做了这个查询

mysql> INSERT INTO test (a,b) SELECT a,b FROM test_current;
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 1

mysql> select * from test;
+-----+------+------+
| cnt | a | b |
+-----+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
+-----+------+------+
2 rows in set (0.00 sec)

但是当我再次查询的时候

mysql> INSERT INTO test (a,b) SELECT a,b FROM test_current;
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 1

mysql> select * from test;
+-----+------+------+
| cnt | a | b |
+-----+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 4 | 1 | 1 |
| 5 | 2 | 2 |
+-----+------+------+

自动递增只是跳过了 cnt for 3。我想知道这是怎么回事。

最佳答案

每次将值插入表之前,您都可以将 auto_increment 值重置为 1:

ALTER TABLE `test` AUTO_INCREMENT = 1;
INSERT INTO test (a,b) SELECT a,b FROM test_current;

关于插入选择时Mysql自动增量跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12226105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com