简体   繁体   English

SELECT LAST_INSERT_ID()

[英]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: 如果符合以下条件,则无效

  1. You provide the explicit value for the AUTO_INCREMENT column 您为AUTO_INCREMENT列提供显式值
  2. You call LAST_INSERT_ID in another session 您在另一个会话中调用LAST_INSERT_ID
  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). 您在同一语句中插入多行( 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: 以下是您可以尝试的其他一些事项:

  1. Make sure you read the result as an integer. 确保以整数形式读取结果。 This shows an analogous case that required explicit conversion to Int32. 显示了一个类似的情况,需要显式转换为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 . 如果您正在进行多次插入,请知道只将第一个插入的元组的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.

相关问题 spring jdbctemplate为last_insert_id()返回0 - spring jdbctemplate returns 0 for last_insert_id() Java Spring JDBC last_insert_id() - Java Spring JDBC last_insert_id() INSERT Batch之后我可以用LAST_INSERT_ID()进行SELECT选择吗? - Can I SELECT with LAST_INSERT_ID() after INSERT Batch without any cares? 如何使用prepareStatement插入last_insert_id? - How do I insert last_insert_id using preparedStatement? mysql last_insert_id()是否适用于连接池? - Does mysql last_insert_id() work for connection pool? MySQL last_insert_id()在正确的事务中返回0 - MySQL last_insert_id( ) returns 0 within the proper transaction 获取 mysql(innodb) AUTO_INCREMENT Column、JDBC/getGeneratedKeys()/last_insert_id (in OkPacket) 和 LAST_INSERT_ID() 的方法 - Ways to get mysql(innodb) AUTO_INCREMENT Column, JDBC/getGeneratedKeys()/last_insert_id (in OkPacket) and LAST_INSERT_ID() MYSQL LAST_INSERT_ID无法正常工作。 我该如何解决? - MYSQL LAST_INSERT_ID not working as intended. How do I fix? 在 Apache Derby Embedded 上调用 last_insert_id() 时出现休眠异常 - Hibernate exception when last_insert_id() called on Apache Derby Embedded eclipselink自动生成的密钥:sql server last_insert_id&#39;不是公认的内置函数名称 - eclipselink auto generated key : sql server last_insert_id' is not a recognized built-in function name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM