English 中文(简体)
SlectT LAST_INSERT_ID()
原标题:SELECT LAST_INSERT_ID()

有些人可以解释一下MySQL如何运作LAST_INSERT_ID(ID)。 I m试图在数据库中 last上最后插入一行,但每次都有1台。

我使用我的施舍。

例:

<insert id="insertInto" parameterType="Something" timeout="0">
  INSERT INTO something (something) VALUES (#{something})
  <selectKey resultType="int">
    SELECT LAST_INSERT_ID()
  </selectKey>
</insert>

法典:

System.out.println("Id : " + id)

产出:

Id : 1
问题回答

<代码>LAST_INSERT_ID 回到最后数值简单明了插入。AUTO_INCREMENT 页: 1

CREATE TABLE mytable (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, value INT NOT NULL);

为了使该栏自动增加,请从<代码>中删除。 INSERT list:

INSERT
INTO    mytable (value)
VALUES  (1)

或向其提供<条码>。 价值:

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

之后,

SELECT  LAST_INSERT_ID()

页: 1 缩略语

如果:

  1. You provide the explicit value for the AUTO_INCREMENT column
  2. You call LAST_INSERT_ID in 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).
LAST_INSERT_ID()

每个用户和每个链接。

可在

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!

例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.

例:

SELECT LAST_INSERT_ID() FROM my_table;




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签