婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

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

熱門標(biāo)簽:手機(jī)外呼系統(tǒng)違法嗎 桂林云電銷機(jī)器人收費(fèi) 谷歌地圖標(biāo)注位置圖解 南通電銷外呼系統(tǒng)哪家強(qiáng) 東莞外呼企業(yè)管理系統(tǒng) 地圖簡圖標(biāo)注 沈陽智能外呼系統(tǒng)供應(yīng)商 清遠(yuǎn)申請400電話 如何選擇優(yōu)質(zhì)的外呼系統(tǒng)

前言

最近一個同事問我,為什么last_insert_id()得到的結(jié)果與預(yù)期的不一樣呢,于是我就認(rèn)真的去研究的一下這個參數(shù),下面是關(guān)于last_insert_id()的詳細(xì)介紹,一起來學(xué)習(xí)學(xué)習(xí)吧。

首先,舉個例子

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()符合預(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么,為什么是第一個插入的值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()返回的是最近一次針對autoincrement列執(zhí)行的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語句第一個自動生成的值。

然后,剖析一下

請認(rèn)真閱讀上述翻譯中的黑色字體,牢記last_insert_id()的約束。

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

官方文檔明明說了,是自動生成的值啊,不是你指定的值啊,是由autoincremnt計(jì)數(shù)器自己生成的才能被last_insert_id()追蹤到哇。。

為什么多值插入的時候,顯示的是第一條插入值啊,last不是最后一個值的意思么啊啊啊。。

官方文檔明明說了,是最近一次的INSERT語句**自動生成的第一個值**哇哇哇。。

總結(jié)

記住last_insert_id()的約束。最近一次INSERT語句在autpincrement列上自動生成的第一個值。總結(jié)的這句話比翻譯的那句話感覺順口多了==

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

您可能感興趣的文章:
  • Mysql中LAST_INSERT_ID()的函數(shù)使用詳解
  • Mysql字符串截取函數(shù)SUBSTRING的用法說明
  • MySQL replace函數(shù)替換字符串語句的用法
  • FROM_UNIXTIME 格式化MYSQL時間戳函數(shù)
  • mysql獲取字符串長度函數(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)簽:成都 內(nèi)蒙古 臨沂 天津 重慶 湖州 常德 貴州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《輕松掌握MySQL函數(shù)中的last_insert_id()》,本文關(guān)鍵詞  輕松,掌握,MySQL,函數(shù),中的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《輕松掌握MySQL函數(shù)中的last_insert_id()》相關(guān)的同類信息!
  • 本頁收集關(guān)于輕松掌握MySQL函數(shù)中的last_insert_id()的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 乌拉特后旗| 土默特左旗| 饶阳县| 宾川县| 水城县| 陆川县| 云安县| 巴楚县| 海林市| 高要市| 综艺| 含山县| 拉萨市| 盐城市| 宜都市| 衡山县| 三亚市| 长顺县| 兴隆县| 石泉县| 罗田县| 从江县| 宁德市| 元氏县| 茶陵县| 通化县| 遂平县| 元阳县| 陆丰市| 搜索| 怀仁县| 天柱县| 梁河县| 贵定县| 田东县| 穆棱市| 邻水| 泰和县| 孙吴县| 通道| 铜川市|