《Mysql應用輕松掌握MySQL函數中的last_insert_id()》要點:
本文介紹了Mysql應用輕松掌握MySQL函數中的last_insert_id(),希望對您有用。如果有疑問,可以聯系我們。
MYSQL教程前言
MYSQL教程最近一個同事問我,為什么last_insert_id()
得到的結果與預期的不一樣呢,于是我就認真的去研究的一下這個參數,下面是關于last_insert_id()
的詳細介紹,一起來學習學習吧.
MYSQL教程首先,舉個例子
MYSQL教程
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教程其次,研究一下
MYSQL教程查閱MySQL官方文檔,真的太重要了...
MYSQL教程官方出處:http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
MYSQL教程官方文檔原話:
MYSQL教程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.
MYSQL教程翻譯:
MYSQL教程沒有參數的last_insert_id()
返回的是最近一次針對autoincrement列執行的INSERT
語句的第一個自動生成的值.
MYSQL教程官方文檔原話:
MYSQL教程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.
MYSQL教程翻譯:
MYSQL教程如果你在單條INSERT
語句中插入多個值,那么last_insert_id()
返回的是該INSERT
語句第一個自動生成的值.
MYSQL教程然后,剖析一下
MYSQL教程請認真閱讀上述翻譯中的黑色字體,牢記last_insert_id()
的約束.
MYSQL教程為什么插入指定的值,last_insert_id()
就失效了呢?
MYSQL教程官方文檔明明說了,是自動生成的值啊,不是你指定的值啊,是由autoincremnt計數器自己生成的才能被last_insert_id()
追蹤到哇..
MYSQL教程為什么多值插入的時候,顯示的是第一條插入值啊,last
不是最后一個值的意思么啊啊啊..
MYSQL教程官方文檔明明說了,是最近一次的INSERT
語句**自動生成的第一個值**哇哇哇..
MYSQL教程總結
MYSQL教程記住last_insert_id()
的約束.最近一次INSERT
語句在autpincrement列上自動生成的第一個值.總結的這句話比翻譯的那句話感覺順口多了==
MYSQL教程好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的贊助,如果有疑問大家可以留言交流.
歡迎參與《Mysql應用輕松掌握MySQL函數中的last_insert_id()》討論,分享您的想法,維易PHP學院為您提供專業教程。