加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

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

发布时间:2020-12-12 02:52:57 所属栏目:MySql教程 来源:网络整理
导读:《Mysql应用轻松掌握MySQL函数中的last_insert_id()》要点: 本文介绍了Mysql应用轻松掌握MySQL函数中的last_insert_id(),希望对您有用。如果有疑问,可以联系我们。 MYSQL教程 前言 MYSQL教程 最近一个同事问我,为什么 last_insert_id() 得到的结果与预期

《Mysql应用轻松掌握MySQL函数中的last_insert_id()》要点:
本文介绍了Mysql应用轻松掌握MySQL函数中的last_insert_id(),希望对您有用。如果有疑问,可以联系我们。

MYSQL教程前言

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

MYSQL教程首先,举个例子

MYSQL教程
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(),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教程其次,研究一下

MYSQL教程查阅MySQL官方文档,真的太重要了...

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

MYSQL教程官方文档原话:

MYSQL教程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.

MYSQL教程翻译:

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

MYSQL教程官方文档原话:

MYSQL教程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.

MYSQL教程翻译:

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

MYSQL教程然后,剖析一下

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

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

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

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

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

MYSQL教程总结

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

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

欢迎参与《Mysql应用轻松掌握MySQL函数中的last_insert_id()》讨论,分享您的想法,编程之家PHP学院为您提供专业教程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读