java 选择 LAST_INSERT_ID()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10294122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SELECT LAST_INSERT_ID()
提问by Staba
Can somebody explain how works MySQL function LAST_INSERT_ID(). I'm trying to get id of last inserted row in database, but every time get 1.
有人可以解释一下 MySQL 函数 LAST_INSERT_ID() 是如何工作的。我正在尝试获取数据库中最后插入行的 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
回答by Quassnoi
LAST_INSERT_ID
returns the last value implicitlyinserted 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:
如果出现以下情况,这将不起作用:
- You provide the explicit value for the
AUTO_INCREMENT
column - You call
LAST_INSERT_ID
in another session - You insert more than one row in the same statement (
LAST_INSERT_ID()
will return the value of the first row inserted, not the last one).
- 您为
AUTO_INCREMENT
列提供显式值 - 你
LAST_INSERT_ID
在另一个会话中调用 - 在同一个语句中插入多行(
LAST_INSERT_ID()
将返回插入的第一行的值,而不是最后一行)。
回答by Silviu
LAST_INSERT_ID()
is per user and per connection.
是每个用户和每个连接。
You can read more in MySQL doc.
您可以在MySQL doc 中阅读更多内容。
回答by zloctb
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!!!
回答by Karthik. V
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. You need to let the DB set it for itself.
还要确保您没有为该auto_increment
领域提供明确的价值。您需要让数据库自行设置。
Here are some other things you could try:
以下是您可以尝试的其他一些事情:
回答by Staba
I have two solutions, after implements much complicated i find out about second one...
I will tell you second which is better one ...
It's pretty simple... just in query insert this keyProperty="id"
Like this :
<insert id="insertInto" parameterType="Something" keyProperty="id" timeout="0">
INSERT INTO something (something) VALUES (#{something})
</insert>
Query returns id of inserted row
Thanks!
我有两个解决方案,在实现非常复杂之后我找到了第二个......我会告诉你第二个哪个更好......这很简单......只是在查询中插入这个keyProperty="id"
像这样:
<insert id="insertInto" parameterType="Something" keyProperty="id" timeout="0">
INSERT INTO something (something) VALUES (#{something})
</insert>
查询返回插入行的id谢谢!
回答by Rim
You have to use a Table name to select the last insert id.
您必须使用表名称来选择最后一个插入 ID。
Example:
例子:
SELECT LAST_INSERT_ID() FROM my_table;