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

主頁 > 知識(shí)庫(kù) > 輕松掌握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) 地圖簡(jiǎn)圖標(biāo)注 沈陽智能外呼系統(tǒng)供應(yīng)商 清遠(yuǎn)申請(qǐng)400電話 如何選擇優(yōu)質(zhì)的外呼系統(tǒng)

前言

最近一個(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è)值。總結(jié)的這句話比翻譯的那句話感覺順口多了==

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

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

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《輕松掌握MySQL函數(shù)中的last_insert_id()》,本文關(guān)鍵詞  輕松,掌握,MySQL,函數(shù),中的,;如發(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)文章
  • 下面列出與本文章《輕松掌握MySQL函數(shù)中的last_insert_id()》相關(guān)的同類信息!
  • 本頁收集關(guān)于輕松掌握MySQL函數(shù)中的last_insert_id()的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 射洪县| 舟山市| 静乐县| 密山市| 历史| 师宗县| 伊春市| 隆德县| 牡丹江市| 离岛区| 蒙山县| 五常市| 炉霍县| 高唐县| 毕节市| 昌平区| 安图县| 平乐县| 巧家县| 绵竹市| 金塔县| 宝兴县| 黄石市| 四平市| 买车| 汉阴县| 郸城县| 光泽县| 古浪县| 沾化县| 图们市| 清原| 忻城县| 离岛区| 沙田区| 明水县| 汝城县| 浠水县| 寻乌县| 建宁县| 石楼县|