程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> 輕松掌握MySQL函數中的last_insert_id()

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

編輯:關於MYSQL數據庫

前言

最近一個同事問我,為什麼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: 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)
# 納尼?按照預期不是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列上自動生成的第一個值。總結的這句話比翻譯的那句話感覺順口多了==

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved