[英]SELECT LAST_INSERT_ID()
Can somebody explain how works MySQL function LAST_INSERT_ID(). 有人可以解释MySQL函数LAST_INSERT_ID()的工作原理。 I'm trying to get id of last inserted row in database, but every time get 1.
我想在数据库中获取最后一行插入的id,但每次都得到1。
I use mybatis. 我用mybatis。
Example query is : 示例查询是:
<insert id="insertInto" parameterType="Something" timeout="0">
INSERT INTO something (something) VALUES (#{something})
<selectKey resultType="int">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
Code: 码:
System.out.println("Id : " + id)
Output: 输出:
Id : 1
LAST_INSERT_ID
returns the last value implicitly inserted into an AUTO_INCREMENT
column in the current session. LAST_INSERT_ID
返回隐式插入当前会话中AUTO_INCREMENT
列的最后一个值。
CREATE TABLE mytable (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, value INT NOT NULL);
To make the column autoincrement, you should omit it from the INSERT
list: 要使列自动增量,您应该从
INSERT
列表中省略它:
INSERT
INTO mytable (value)
VALUES (1)
or provide it with a NULL
value: 或者为它提供
NULL
值:
INSERT
INTO mytable (id, value)
VALUES (NULL, 1)
After that, 之后,
SELECT LAST_INSERT_ID()
will return you the value AUTO_INCREMENT
has inserted into the id
column. 将返回
AUTO_INCREMENT
已插入id
列的值。
This will not work if: 如果符合以下条件,则无效
AUTO_INCREMENT
column AUTO_INCREMENT
列提供显式值 LAST_INSERT_ID
in another session LAST_INSERT_ID
LAST_INSERT_ID()
will return the value of the first row inserted, not the last one). LAST_INSERT_ID()
将返回插入的第一行的值,而不是最后一行的值)。 LAST_INSERT_ID()
is per user and per connection. 是按用户和每个连接。
You can read more in MySQL doc . 您可以在MySQL doc中阅读更多内容。
I haven't used MyBatis yet, but after you insert something into the table, check that the auto_increment value is being updated with the following MySQL query: 我还没有使用过MyBatis,但是在向表中插入内容之后,请检查auto_increment值是否正在使用以下MySQL查询进行更新:
SELECT Auto_increment FROM (SHOW TABLE STATUS LIKE '<table-name>') as A;
Also make sure that you have no explicit value you're giving to the auto_increment
field. 还要确保您没有给
auto_increment
字段赋予明确的值。 You need to let the DB set it for itself. 您需要让DB为自己设置它。
Here are some other things you could try: 以下是您可以尝试的其他一些事项:
Make sure you read the result as an integer. 确保以整数形式读取结果。 This shows an analogous case that required explicit conversion to Int32.
这显示了一个类似的情况,需要显式转换为Int32。
If you're doing multiple inserts, know that only the ID of the first inserted tuple is treated as the LAST_INSERT_ID
. 如果您正在进行多次插入,请知道只将第一个插入的元组的ID视为
LAST_INSERT_ID
。 According to this (Search for use test
). 根据这个 (搜索
use test
)。
我有两个解决方案,在实现了很复杂之后我发现了第二个...我会告诉你第二个哪个更好...这很简单...只是在查询中插入此keyProperty="id"
像这样: <insert id="insertInto" parameterType="Something" keyProperty="id" timeout="0"> INSERT INTO something (something) VALUES (#{something}) </insert>
查询返回插入行的id谢谢!
Example 1: 例1:
mysql> CREATE TABLE prime2 LIKE prime;
Query OK, 0 rows affected (0.08 sec)
mysql> SELECT LAST_INSERT_ID(); //From table prime!!!
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 3 |
+------------------+
1 row in set (0.00 sec)
mysql> INSERT INTO prime2 VALUES(1,1);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 3 |
+------------------+
1 row in set (0.00 sec) //From table prime!!!
You have to use a Table name to select the last insert id. 您必须使用表名来选择最后一个插入ID。
Example: 例:
SELECT LAST_INSERT_ID() FROM my_table;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.