Questions tagged [last-insert-id]

LAST_INSERT_ID is MySQL specific functionality to get the value of the AUTO_INCREMENT column most recently inserted into. It is the recommended means of getting the value in MySQL, because SELECT MAX(auto_increment) ... is not reliable, as concurrency problems can occur.

LAST_INSERT_ID is MySQL specific functionality to get the value of the AUTO_INCREMENT column most recently inserted into. It is the recommended means of getting the value in MySQL, because SELECT MAX(auto_increment) ... is not reliable, as concurrency problems can occur.

That said, this functionality is not ANSI. Sequences are now ANSI, supported by DB2, Oracle, PostgreSQL, and SQL Server "Denali"). The ANSI equivalent to LAST_INSERT_ID would be: CURRVAL ( NEXTVAL is used to get the next value).

Documentation:

215 questions
180
votes
14 answers

LAST_INSERT_ID() MySQL

I have a MySQL question that I think must be quite easy. I need to return the LAST INSERTED ID from table1 when I run the following MySql query: INSERT INTO table1 (title,userid) VALUES ('test',1); INSERT INTO table2 (parentid,otherid,userid)…
Martin
  • 2,163
  • 6
  • 21
  • 16
27
votes
5 answers

Get the id of inserted row using C#

I have a query to insert a row into a table, which has a field called ID, which is populated using an AUTO_INCREMENT on the column. I need to get this value for the next bit of functionality, but when I run the following, it always returns 0 even…
Elie
  • 13,693
  • 23
  • 74
  • 128
16
votes
5 answers

Clearing LAST_INSERT_ID() before inserting to tell if what's returned is from my insert

LAST_INSERT_ID() returns the most recent id generated for the current connection by an auto increment column, but how do I tell if that value is from the last insert and not from a previous insert on the same connection? Suppose I am using a…
Bohemian
  • 412,405
  • 93
  • 575
  • 722
15
votes
2 answers

PHP Postgres: Get Last Insert ID

I found a couple of other questions on this topic. This one... mysql_insert_id alternative for postgresql ...and the manual seem to indicate that you can call lastval() any time and it will work as expected. But this one... Postgresql and PHP: is…
Jonah
  • 9,991
  • 5
  • 45
  • 79
15
votes
6 answers

Mysql get last id of specific table

I have to get last insert id from a specific inserted table?. Lets say i have this code: INSERT INTO blahblah (test1, test 2) VALUES ('test1', 'test2'); INSERT INTO blahblah2 (test1, test 2) VALUES ('test1', 'test2'); INSERT INTO blahblah3 (test1,…
simon
  • 2,235
  • 6
  • 33
  • 53
12
votes
2 answers

How to get inserted id using Spring Jdbctemplate.update(String sql, obj...args)

I'm using Jdbctemplate and I need the inserted id of a query. I read that I have to build a particular PreparedStatement and use GeneratedKeyHolder object. The problem is that in my application all inserts method uses this JdbcTemplate update…
Tobia
  • 9,165
  • 28
  • 114
  • 219
11
votes
5 answers

When I INSERT multiple rows into a MySQL table, will the ids be increment by 1 everytime?

if I have a query like the following: INSERT INTO table (col1,col2,col3) VALUES ('col1_value_1', 'col2_value_1', 'col3_value_1'), ('col1_value_2', 'col2_value_2', 'col3_value_2'), ('col1_value_3', 'col2_value_3', 'col3_value_3'); Suppose that I…
tonix
  • 6,671
  • 13
  • 75
  • 136
11
votes
9 answers

PDO - lastInsertId() for insert query with multiple rows

I can insert 2 pets into a table, and get their lastInsertId() for further processing one at a time (2 queries). I am wondering if there is a way to get two lastInsertIds() and assign them to variables if I am inserting 2 rows in 1 query: $query =…
Maverick
  • 1,123
  • 5
  • 16
  • 30
8
votes
2 answers

how to get insert_id using mysqli prepare statements

How can I get the insert_id of the last record using mysqli prepare statement, for example following? $stmt = $this->mysqli->prepare("INSERT INTO test (name) values (?)") $stmt->bind_param('s', $name); $stmt->execute();
Ajay
  • 1,107
  • 2
  • 13
  • 12
8
votes
3 answers

Using Mysql to do multiple INSERT on linked tables

I have two tables, one linked to the Primary Key of the other. At the moment I INSERT into table A, get the LAST_INSERT_ID, and then INSERT into table B. But I have hundreds of records to insert and I want to speed things up. In Mysql you can…
Michael Franze
  • 435
  • 1
  • 6
  • 14
7
votes
1 answer

mysql_insert_id and last_insert_id wrong behavior

I have this table CREATE TABLE IF NOT EXISTS `t5` ( `id` int(11) NOT NULL auto_increment, `a` int(11) NOT NULL, `b` int(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `a` (`a`,`b`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ; a_b is a…
Matt.Z
  • 602
  • 7
  • 19
7
votes
2 answers

How can I retrieve instance of last added item

I'm using github.com/jinzhu/gorm with a mysql backend. I want to retrieve the Id (or the full entity) of the row in the previous Create call. As in, last-insert-id:…
sathishvj
  • 1,394
  • 2
  • 17
  • 28
7
votes
1 answer

Alter the LAST_INSERT_ID() from within a TRIGGER in MySQL

I have a BEFORE INSERT TRIGGER which is used to calculate the AUTO_INCREMENT value of a column (id_2). id_1 | id_2 | data 1 | 1 | 'a' 1 | 2 | 'b' 1 | 3 | 'c' 2 | 1 | 'a' 2 | 2 | 'b' 2 | 3 | 'c' 2 | 4 | 'a' 3…
Xeos
  • 5,975
  • 11
  • 50
  • 79
6
votes
2 answers

Why doesn't PDO's Oracle driver implement lastInsertId()?

I get this error in PDO: error: Message: PDO::lastInsertId() [pdo.lastinsertid]: SQLSTATE[IM001]: Driver does not support this function: driver does not support lastInsertId() when trying to get last inserted id from an oracle database. I…
Mythriel
  • 1,360
  • 5
  • 24
  • 45
6
votes
5 answers

How to retrieve all last inserted rows IDs in mysql-php?

Is there any efficient way to retrieve all last inserted rows auto incremented IDs from mysql database using php? Suppose I don't know how many rows are there to be inserted in a batch but I need all of their IDs after insertion. After Googling I…
leevi
  • 91
  • 1
  • 7
1
2 3
14 15