首頁  >  文章  >  資料庫  >  輕鬆掌握MySQL函數中的last_insert_id()

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

黄舟
黄舟原創
2017-01-18 11:17:191486瀏覽

Mysql關係型資料庫管理系統

MySQL是一個開放式原始碼的小型關聯式資料庫管理系統,開發者為瑞典MySQL AB公司。 MySQL被廣泛地應用在Internet上的中小型網站。由於其體積小、速度快、總體擁有成本低,尤其是開放原始碼這一特點,許多中小型網站為了降低網站總體擁有成本而選擇了MySQL作為網站資料庫。


相信大家應該都知道Mysql函數可以實現許多我們需要的功能,這篇文章介紹的Mysql函數Last_insert_id()就是其中之一,文章透過一個例子展開來講,應該更有助於大家的理解和學習,有需要的朋友們下面來一起看看吧。

前言

最近有同事問我,為什麼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欄位上自動產生的第一個值。總結的這句話比翻譯的那句話感覺順口多了==

好了,以上就是這篇文章的全部內容了,更多相關內容請關注PHP中文網(www.php.cn)!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn