MySQL/自增主键:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第3行: 第3行:
查询自增主键id
查询自增主键id
=== LAST_INSERT_ID ===
=== LAST_INSERT_ID ===
{{Shc_out|<syntaxhighlight lang="bash">
{{Shc_out|<syntaxhighlight lang="mysql">
SELECT LAST_INSERT_ID();</syntaxhighlight>|<syntaxhighlight lang="bash">
SELECT LAST_INSERT_ID();</syntaxhighlight>|<syntaxhighlight lang="bash">
+------------------+
+------------------+
第11行: 第11行:
+------------------+
+------------------+
1 row in set (0.00 sec)
1 row in set (0.00 sec)
</syntaxhighlight>|>mysql}}
</syntaxhighlight>|mysql>}}


<div class="pageBoxGreen">LAST_INSERT_ID 基于 Connection。</div>
<div class="pageBoxGreen">LAST_INSERT_ID 基于 Connection。</div>

2022年2月7日 (一) 08:02的版本

标题文字

自增主键id

查询自增主键id

LAST_INSERT_ID

mysql>
SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)
LAST_INSERT_ID 基于 Connection。

max

[jian@kang /home/kangkk]$
select max(id) from user;
+---------+
| max(id) |
+---------+
|      14 |
+---------+
1 row in set (0.01 sec)

存储过程

>mysql
DELIMITER $$
DROP PROCEDURE IF EXISTS `test` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `test`(in name varchar(100), out oid int)
BEGIN
  select max(id) from user into oid;
  select oid;
END $$
DELIMITER ;
call test('', @id);
+------+
| oid  |
+------+
|   14 |
+------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

identity

[jian@kang /home/kangkk]$
select @@IDENTITY;
+------------+
| @@IDENTITY |
+------------+
|          0 |
+------------+
1 row in set (0.00 sec)