当 LAST_INSERT_ID() 带有参数时# 清空重来

[[email protected]]> truncate table t;

# 插入1条新记录
[[email protected]]> insert into t select 0,rand()*1024;

# 查看 last_insert_id(), 符合预期
[[email protected]]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+

[[email protected]]> select * from t;
+----+-----+
| id | c1 |
+----+-----+
| 1 | 730 |
+----+-----+

# 调用 last_insert_id() 时增加表达式
[[email protected]]> select last_insert_id(id+2) from t;
+----------------------+
| last_insert_id(id+2) |
+----------------------+
| 3 |
+----------------------+

# 再看 last_insert_id() 的值,好像“又不符合预期”了
[[email protected]]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 3 |
+------------------+

# 插入1条新纪录
[[email protected]]> insert into t select 0,rand()*1024;

# 再看 last_insert_id() 的值,好像“又回到正轨”了
[[email protected]]> select last_insert_id();
+------------------+
| last_insert_id(http://www.amjmh.com) |
+------------------+
| 2 |
+------------------+

[[email protected]]> select * from t;
+----+-----+
| id | c1 |
+----+-----+
| 1 | 730 |
| 2 | 600 |
+----+-----+
2 rows in set (0.00 sec)

posted on 2019-09-02 19:11  激流勇进1  阅读(259)  评论(0)    收藏  举报