在 MySQL 中 select @@identity 起什么作用?
mysqlmysqli database更新于 2025/12/25 6:36:17
@@identity 返回当前会话中 auto_increment 列中最后插入的值。让我们首先创建一个表 −
mysql> create table DemoTable( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserName varchar(100) ); Query OK, 0 rows affected (0.67 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable(UserName) values('David');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(UserName) values('Mike');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(UserName) values('Carol');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(UserName) values('Robert');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable(UserName) values('John');
Query OK, 1 row affected (0.13 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+--------+----------+ | UserId | UserName | +--------+----------+ | 1 | David | | 2 | Mike | | 3 | Carol | | 4 | Robert | | 5 | John | +--------+----------+ 5 rows in set (0.00 sec)
现在,我们将使用 @@identity −
mysql> select @@identity;
这将产生以下输出 −
+------------+ | @@identity | +------------+ | 5 | +------------+ 1 row in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

