gpt4 book ai didi

轻松掌握MySQL函数中的last_insert_id()

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章轻松掌握MySQL函数中的last_insert_id()由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

前言 。

最近一个同事问我,为什么last_insert_id()得到的结果与预期的不一样呢,于是我就认真的去研究的一下这个参数,下面是关于last_insert_id()的详细介绍,一起来学习学习吧.

首先,举个例子 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
wing@3306>show create table tt;
+ -------+-----------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                           |
+ -------+-----------------------------------------------------------------------------------------------------------------------+
| tt | CREATE TABLE `tt` (
  `id` int (11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+ -------+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
# 没有指定值的时候,last_insert_id()符合预期希望
wing@3306> insert into tt values ();
Query OK, 1 row affected (0.00 sec)
wing@3306> select last_insert_id();
+ ------------------+
| last_insert_id() |
+ ------------------+
|    1 |
+ ------------------+
1 row in set (0.00 sec)
wing@3306> insert into tt values ();
Query OK, 1 row affected (0.00 sec)
wing@3306> select last_insert_id();
+ ------------------+
| last_insert_id() |
+ ------------------+
|    2 |
+ ------------------+
1 row in set (0.00 sec)
# what?不是应该是5么,为什么是第一个插入的值3?last_insert_id开始有一点不符合预期了。。
wing@3306> insert into tt values (),(),();
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
wing@3306> select last_insert_id();
+ ------------------+
| last_insert_id() |
+ ------------------+
|    3 |
+ ------------------+
1 row in set (0.00 sec)
wing@3306> insert into tt values (),(),();
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
wing@3306> select last_insert_id();
+ ------------------+
| last_insert_id() |
+ ------------------+
|    6 |
+ ------------------+
1 row in set (0.00 sec)
# 纳尼?按照预期不是10么?为什么还是之前的6?last_insert_id()我不懂你啊。。
wing@3306> insert into tt values (10);
Query OK, 1 row affected (0.01 sec)
wing@3306> select last_insert_id();
+ ------------------+
| last_insert_id() |
+ ------------------+
|    6 |
+ ------------------+
1 row in set (0.00 sec)

其次,研究一下 。

查阅MySQL官方文档,真的太重要了。。.

官方出处:http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id 。

官方文档原话:

With no argument, LAST_INSERT_ID() returns a 64-bit value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement. 。

翻译:

没有参数的last_insert_id()返回的是最近一次针对autoincrement列执行的INSERT语句的第一个自动生成的值.

官方文档原话:

If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only. The reason for this is to make it possible to reproduce easily the same INSERT statement against some other server. 。

翻译:

如果你在单条INSERT语句中插入多个值,那么last_insert_id()返回的是该INSERT语句第一个自动生成的值.

然后,剖析一下 。

请认真阅读上述翻译中的黑色字体,牢记last_insert_id()的约束.

为什么插入指定的值,last_insert_id()就失效了呢?

官方文档明明说了,是自动生成的值啊,不是你指定的值啊,是由autoincremnt计数器自己生成的才能被last_insert_id()追踪到哇。.

为什么多值插入的时候,显示的是第一条插入值啊,last不是最后一个值的意思么啊啊啊。.

官方文档明明说了,是最近一次的INSERT语句**自动生成的第一个值**哇哇哇。.

总结 。

记住last_insert_id()的约束。最近一次INSERT语句在autpincrement列上自动生成的第一个值。总结的这句话比翻译的那句话感觉顺口多了== 。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流.

最后此篇关于轻松掌握MySQL函数中的last_insert_id()的文章就讲到这里了,如果你想了解更多关于轻松掌握MySQL函数中的last_insert_id()的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

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