輕松掌握MySQL函數(shù)中的last_insert_id()
前言
最近一個(gè)同事問我,為什么last_insert_id()
得到的結(jié)果與預(yù)期的不一樣呢,于是我就認(rèn)真的去研究的一下這個(gè)參數(shù),下面是關(guān)于last_insert_id()
的詳細(xì)介紹,一起來學(xué)習(xí)學(xué)習(xí)吧。
首先,舉個(gè)例子
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) # 沒有指定值的時(shí)候,last_insert_id()符合預(yù)期希望 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?不是應(yīng)該是5么,為什么是第一個(gè)插入的值3?last_insert_id開始有一點(diǎn)不符合預(yù)期了。。 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) # 納尼?按照預(yù)期不是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.
翻譯:
沒有參數(shù)的last_insert_id()
返回的是最近一次針對(duì)autoincrement列執(zhí)行的INSERT
語句的第一個(gè)自動(dòng)生成的值。
官方文檔原話:
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
語句中插入多個(gè)值,那么last_insert_id()
返回的是該INSERT
語句第一個(gè)自動(dòng)生成的值。
然后,剖析一下
請(qǐng)認(rèn)真閱讀上述翻譯中的黑色字體,牢記last_insert_id()
的約束。
為什么插入指定的值,last_insert_id()
就失效了呢?
官方文檔明明說了,是自動(dòng)生成的值啊,不是你指定的值啊,是由autoincremnt計(jì)數(shù)器自己生成的才能被last_insert_id()
追蹤到哇。。
為什么多值插入的時(shí)候,顯示的是第一條插入值啊,last
不是最后一個(gè)值的意思么啊啊啊。。
官方文檔明明說了,是最近一次的INSERT
語句**自動(dòng)生成的第一個(gè)值**哇哇哇。。
總結(jié)
記住last_insert_id()
的約束。最近一次INSERT
語句在autpincrement列上自動(dòng)生成的第一個(gè)值??偨Y(jié)的這句話比翻譯的那句話感覺順口多了==
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Navicat for Mysql 字段注釋中文亂碼問題及解決
這篇文章主要介紹了Navicat for Mysql 字段注釋中文亂碼問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09MYSQL必知必會(huì)讀書筆記第十和十一章之使用函數(shù)處理數(shù)據(jù)
這篇文章主要介紹了MYSQL必知必會(huì)讀書筆記第十和十一章之使用函數(shù)處理數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2016-05-05MySQL登錄、訪問及退出操作實(shí)戰(zhàn)指南
當(dāng)我們要使用mysql時(shí),一定要了解mysql的登錄、訪問及退出,下面這篇文章主要給大家介紹了關(guān)于MySQL登錄、訪問及退出操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10mysql 5.7更改數(shù)據(jù)庫的數(shù)據(jù)存儲(chǔ)位置的解決方法
隨著MySQL數(shù)據(jù)庫存儲(chǔ)的數(shù)據(jù)逐漸變大,已經(jīng)將原來的存儲(chǔ)數(shù)據(jù)的空間占滿了,導(dǎo)致mysql已經(jīng)鏈接不上了。所以要給存放的數(shù)據(jù)換個(gè)地方,下面小編給大家分享mysql 5.7更改數(shù)據(jù)庫的數(shù)據(jù)存儲(chǔ)位置的解決方法,一起看看吧2017-04-04