輕鬆掌握MySQL函數中的last_insert_id()_Mysql

來源:互聯網
上載者:User

前言

最近一個同事問我,為什麼last_insert_id()得到的結果與預期的不一樣呢,於是我就認真的去研究的一下這個參數,下面是關於last_insert_id()的詳細介紹,一起來學習學習吧。

首先,舉個例子

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: 0wing@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: 0wing@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官方文檔,真的太重要了。。。

官方出處: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.

翻譯:

沒有參數的last_insert_id()返回的是最近一次針對autoincrement列執行的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語句第一個自動產生的值。

然後,剖析一下

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

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

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

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

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

總結

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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.