mysql select last_insert_id()函数返回的值

mysql> create table j(id int primary key auto_increment, name varchar(10));

创建表j

插入数据

mysql> insert into j(name) values('wanggiqpg');
Query OK, 1 row affected (0.00 sec)

mysql> select last_insert_id() from j;
+------------------+
| last_insert_id() |
+------------------+
|                1 |
+------------------+
1 row in set (0.00 sec)

再插入一些数据

mysql> insert into j(name) values('wanggiqpg'),('chengyn'),('wanggiqpg'),('chengyn'),('wanggiqpg'),('chengyn'),('wanggiqpg'),('chengyn'),('wanggiqpg') ,('chengyn'),('wanggiqpg'),('chengyn');
mysql> select * from j;
+----+-----------+
| id | name |
+----+-----------+
| 1 | wanggiqpg |
| 2 | wanggiqpg |
| 3 | chengyn |
| 4 | wanggiqpg |
| 5 | chengyn |
| 6 | wanggiqpg |
| 7 | chengyn |
| 8 | wanggiqpg |
| 9 | chengyn |
| 10 | wanggiqpg |
| 11 | chengyn |
| 12 | wanggiqpg |
| 13 | chengyn |
+----+-----------+

 

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                2 |
+------------------+

判断可知

select last_insert_id();得到的是上一条insert或者update语句开始执行时开始的自增ID
posted @ 2018-07-02 17:09  月河  阅读(515)  评论(0编辑  收藏  举报