佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > 輕松掌握MySQL函數(shù)中的last_insert_id()

輕松掌握MySQL函數(shù)中的last_insert_id()

熱門標(biāo)簽:鐵路電話系統(tǒng) 地方門戶網(wǎng)站 Linux服務(wù)器 AI電銷 網(wǎng)站排名優(yōu)化 服務(wù)外包 呼叫中心市場(chǎng)需求 百度競(jìng)價(jià)排名

前言

最近一個(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語(yǔ)句的第一個(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語(yǔ)句中插入多個(gè)值,那么last_insert_id()返回的是該INSERT語(yǔ)句第一個(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語(yǔ)句**自動(dòng)生成的第一個(gè)值**哇哇哇。。

總結(jié)

記住last_insert_id()的約束。最近一次INSERT語(yǔ)句在autpincrement列上自動(dòng)生成的第一個(gè)值??偨Y(jié)的這句話比翻譯的那句話感覺順口多了==

好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

您可能感興趣的文章:
  • Mysql中LAST_INSERT_ID()的函數(shù)使用詳解
  • Mysql字符串截取函數(shù)SUBSTRING的用法說明
  • MySQL replace函數(shù)替換字符串語(yǔ)句的用法
  • FROM_UNIXTIME 格式化MYSQL時(shí)間戳函數(shù)
  • mysql獲取字符串長(zhǎng)度函數(shù)(CHAR_LENGTH)
  • Mysql 數(shù)字類型轉(zhuǎn)換函數(shù)
  • MySQL中的LOCATE和POSITION函數(shù)使用方法
  • mysql 強(qiáng)大的trim() 函數(shù)
  • Mysql的GROUP_CONCAT()函數(shù)使用方法
  • MySQL中g(shù)roup_concat函數(shù)深入理解

標(biāo)簽:黃山 蘭州 衡水 湘潭 湖南 崇左 銅川 仙桃

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《輕松掌握MySQL函數(shù)中的last_insert_id()》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    夹江县| 三台县| 庄浪县| 德州市| 普安县| 深圳市| 额尔古纳市| 忻城县| 库车县| 宽甸| 眉山市| 江津市| 酒泉市| 彭山县| 阿瓦提县| 松江区| 紫阳县| 贵港市| 抚松县| 张北县| 桑植县| 罗甸县| 宜兴市| 康保县| 彭水| 梅州市| 库车县| 华安县| 汨罗市| 银川市| 南宁市| 桂东县| 桓台县| 全椒县| 凌源市| 海晏县| 花莲市| 五指山市| 桃江县| 无极县| 太原市|