当 LAST_INSERT_ID() 带有参数时# 清空重来
[root@yejr.me]> truncate table t;
# 插入1条新记录
[root@yejr.me]> insert into t select 0,rand()*1024;
# 查看 last_insert_id(), 符合预期
[root@yejr.me]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
[root@yejr.me]> select * from t;
+----+-----+
| id | c1 |
+----+-----+
| 1 | 730 |
+----+-----+
# 调用 last_insert_id() 时增加表达式
[root@yejr.me]> select last_insert_id(id+2) from t;
+----------------------+
| last_insert_id(id+2) |
+----------------------+
| 3 |
+----------------------+
# 再看 last_insert_id() 的值,好像“又不符合预期”了
[root@yejr.me]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 3 |
+------------------+
# 插入1条新纪录
[root@yejr.me]> insert into t select 0,rand()*1024;
# 再看 last_insert_id() 的值,好像“又回到正轨”了
[root@yejr.me]> select last_insert_id();
+------------------+
| last_insert_id(http://www.amjmh.com) |
+------------------+
| 2 |
+------------------+
[root@yejr.me]> select * from t;
+----+-----+
| id | c1 |
+----+-----+
| 1 | 730 |
| 2 | 600 |
+----+-----+
2 rows in set (0.00 sec)