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
6
votes
6 answers

SELECT LAST_INSERT_ID()

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. I use mybatis. Example query is : INSERT…
Staba
  • 61
  • 1
  • 1
  • 2
6
votes
3 answers

LAST_INSERT_ID() always returns 0 (RMySQL) - separate connection issue

Original example as found in some post According to this post the following SQL statements should give me a vector 1, 2, 2, 2, 2 in the end: require("RMySQL") con <- dbConnect( dbDriver("MySQL"), db="your_db", user="your_user", …
Rappster
  • 12,762
  • 7
  • 71
  • 120
5
votes
1 answer

PDO Get Multiple Insert Ids

Running the following query using PDO (Actually, I use prepared statements but same problem) INSERT INTO MyTable(MyField) VALUES('Row1'), ('Row2') How can I get the Ids for the records relating to Row1 and Row2? $db->lastInsertId() literally returns…
Basic
  • 26,321
  • 24
  • 115
  • 201
5
votes
5 answers

laravel raw queries last_insert_id

Is there a way to get the last_insert_id when using laravel's raw query? DB::query('INSERT into table VALUES ('stuff') ') This returns true or false. Is there a way to get last_insert_id using raw queries in Laravel?
Sinan
  • 5,819
  • 11
  • 39
  • 66
4
votes
1 answer

Does lastInsertId() get the last id inserted by that script or overall?

I am wondering how exactly does lastInsertId() work. Atm, I am using it like this to get the id of the inserted row so I can use this id in other sections of the code. For example: $stmt = $db->prepare('INSERT INTO image_table (image_name,…
Neel
  • 9,352
  • 23
  • 87
  • 128
4
votes
1 answer

Twisted adbapi: runInteraction last_insert_id()

class MySQL(object): def __init__(self): self.dbpool = adbapi.ConnectionPool( 'MySQLdb', db='dummy', user='root', passwd='', host = 'localhost', cp_reconnect =…
Anil Bhat
  • 77
  • 9
3
votes
6 answers

Getting last record from mysql

I am using mysql and facing some problem. I want to retrieve last row that is inserted. << Below are details >> Below is how I created table. create table maxID (myID varchar(4)) I inserted four values in it as below insert into maxID values…
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
3
votes
1 answer

INSERT doesn't work when followed by SELECT LAST_INSERT_ID

There are a lot of questions about LAST_INSERT_ID() In my case, issue is: When INSERT is followed by SELECT LAST_INSERT_ID() there are no records being inserted INSERT INTO sequences (state) VALUES (0); select LAST_INSERT_ID(); >>> 80 // nothing…
3
votes
3 answers

Is there an equivalent to LAST_INSERT_ID for timestamps?

I love the LAST_INSERT_ID() function of MySQL. I use it all the time to retrieve the id of the just inserted row to return from my stored procedure afterwards. However, now I have a table which has a TIMESTAMP as Primary Key that is set to DEFAULT…
Simon Baars
  • 1,877
  • 21
  • 38
3
votes
1 answer

MySQL SELECT LAST_INSERT_ID() for compound key. Is it possible?

Can I get the LAST INSERT ID() for a compound key in MySQL?
atlacatl
3
votes
3 answers

How to select last_insert_id?

I have an SQL statement that returns more than one value: In my code I then need to call the last ID that was inserted. I have seen some examples about SELECT LAST_INSERT_ID() but they all seems to be for insert statements. I just want to show the…
user123456789
  • 1,914
  • 7
  • 44
  • 100
3
votes
2 answers

INSERT INTO & LAST_INSERT_ID inside Query inside function

Background: This is the first time I've posted a question here, but I have learned a lot from reading others' questions and answers and I really appreciate all of your help! I have spent several hours researching this problem and can't find a…
3
votes
1 answer

PDO how to get auto_increment value from a table

I am creating a calendar in php and grabbing events from a database. We also have to insert events using prepared PDO statements. I can successfully create the new event and insert it into an event table. I also have to link and event with a family…
3
votes
3 answers

MSSQL Trigger - Updating newly inserted record on INSERT

I wish to make a modification (Set Deleted = 1) to rows being inserted into my table CustomerContact if the SELECT statement returns more than 0. I have the following, but it remains untested: CREATE TRIGGER mark_cust_contact_deleted ON…
Luke
  • 22,826
  • 31
  • 110
  • 193
3
votes
2 answers

Insert_id is null when used directly in next prepared statement

Finally getting around to learning prepared statements. I'm getting a maddening error when trying to run a pair of simple inserts: $p_stmt = $mysqli->prepare("INSERT INTO ww_pages (page_key) VALUES (?)"); $p_stmt->bind_param('s',…
CodeMoose
  • 2,964
  • 4
  • 31
  • 56
1
2
3
14 15