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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:25:32  来源:igfitidea点击:

SELECT LAST_INSERT_ID()

javamysqlsqlmybatislast-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_IDreturns the last value implicitlyinserted into an AUTO_INCREMENTcolumn 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 INSERTlist:

要使列自动递增,您应该从INSERT列表中省略它:

INSERT
INTO    mytable (value)
VALUES  (1)

or provide it with a NULLvalue:

或为其提供一个NULL值:

INSERT
INTO    mytable (id, value)
VALUES  (NULL, 1)

After that,

在那之后,

SELECT  LAST_INSERT_ID()

will return you the value AUTO_INCREMENThas inserted into the idcolumn.

将返回您AUTO_INCREMENT插入到id列中的值。

This will not work if:

如果出现以下情况,这将不起作用:

  1. You provide the explicit value for the AUTO_INCREMENTcolumn
  2. You call LAST_INSERT_IDin another session
  3. 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).
  1. 您为AUTO_INCREMENT列提供显式值
  2. LAST_INSERT_ID在另一个会话中调用
  3. 在同一个语句中插入多行(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_incrementfield. You need to let the DB set it for itself.

还要确保您没有为该auto_increment领域提供明确的价值。您需要让数据库自行设置。

Here are some other things you could try:

以下是您可以尝试的其他一些事情:

  1. Make sure you read the result as an integer. Thisshows an analogous case that required explicit conversion to Int32.

  2. If you're doing multiple inserts, know that only the ID of the first inserted tuple is treated as the LAST_INSERT_ID. According to this(Search for use test).

  1. 确保将结果读取为整数。显示了需要显式转换为 Int32 的类似情况。

  2. 如果您要进行多次插入,请知道只有第一个插入的元组的 ID 被视为LAST_INSERT_ID. 根据这个(搜索use test)。

回答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;