Questions tagged [lastinsertid]

135 questions
359
votes
12 answers

PostgreSQL function for last inserted ID

In PostgreSQL, how do I get the last id inserted into a table? In MS SQL there is SCOPE_IDENTITY(). Please do not advise me to use something like this: select max(id) from table
Anton
  • 8,537
  • 10
  • 36
  • 64
76
votes
7 answers

Get the last insert id with doctrine 2?

How can I get the last insert id with doctrine 2 ORM? I didn't find this in the documentation of doctrine, is this even possible?
tom
  • 7,548
  • 12
  • 46
  • 69
73
votes
6 answers

What happens when auto_increment on integer column reaches the max_value in databases?

I am implementing a database application and I will use both JavaDB and MySQL as database. I have an ID column in my tables that has integer as type and I use the databases auto_increment-function for the value. But what happens when I get more than…
Jonas
  • 97,987
  • 90
  • 271
  • 355
42
votes
3 answers

MySQL LAST_INSERT_ID() used with multiple records INSERT statement

If I insert multiple records with a loop that executes a single record insert, the last insert id returned is, as expected, the last one. But if I do a multiple records insert statement: INSERT INTO people (name,age) VALUES ('William',25),…
bogdan
  • 1,179
  • 3
  • 11
  • 18
31
votes
1 answer

How to program a MySQL trigger to insert row into another table?

I'm looking to create a MySQL trigger on a table. Essentially, I'm creating an activity stream and need to log actions by users. When a user makes a comment, I want a database trigger on that table to fire and: Grab the ID of the last inserted row…
Eric
  • 777
  • 2
  • 8
  • 17
26
votes
10 answers

Get last inserted value from sqlite database Android

I am trying to get the last inserted rowid from a sqlite database in Android. I have read a lot of posts about it, but can't get one to work. This is my method: public Cursor getLastId() { return mDb.query(DATABASE_TABLE, new String[]…
kakka47
  • 3,419
  • 8
  • 41
  • 52
25
votes
5 answers

How to get last insert Id in SQLite?

Is there any built in function available in SQLite to fetch last inserted row id. For eg :- In mysql we have LAST_INSERT_ID() this kind of a function. For sqllite any function available for doing the same process. Please help me. Thanks
DEVOPS
  • 16,170
  • 29
  • 91
  • 115
22
votes
3 answers

mysqli last insert id

I would like to associate the image with firstname, lastname...how can I retrieve the last rowand use it to insert to the other table? I tried $image = $mysqli->insert_id; then binding but it doesn't work. Can someone help me out? $image =…
user2926655
  • 269
  • 2
  • 3
  • 7
17
votes
5 answers

PDO lastInsertId() always return 0

I've come across with a problem. My framework was working just fine with PHP 5.3.0. I upgraded my PHP version to PHP 5.4.x and I started to have few issues with some parts of my framework. After PHP version upgrade, PDO lastInsterId() always returns…
Revenant
  • 2,712
  • 7
  • 28
  • 49
16
votes
3 answers

undefined method PDO lastInsertId

I have an insert query, and I want to get the ID from the table. I have been searching, and I found lastInsertId() for PDO. When I want to use it, I get PHP errors. This is my code: $db = new database(); $naam =…
Marnix
  • 273
  • 1
  • 3
  • 13
15
votes
3 answers

How can I get last 50 documents in mongoDB?

How can I get last 50 documents in mongoDB? I have a collection which is made by db.createCollection("collection",{capped:true, size:300000}); from this "collection" I would like to have last 50 documents instead of get first 50 documents. I know…
jwchang
  • 9,797
  • 15
  • 53
  • 86
13
votes
1 answer

mysql getting last_insert_id() in a trigger

It's my understanding that when you call last_insert_id() it goes by connections, so you'll get the id of the last row inserted on the same connection where last_insert_id() is called, right? So what if I call last_insert_id() in an 'AFTER INSERT'…
Steinthor.palsson
  • 5,618
  • 12
  • 39
  • 50
13
votes
1 answer

MySQL and the chance of the wrong id being returned by LAST_INSERT_ID()

From what I have read LAST_INSERT_ID() retrieves the id of the last insert statement to run. If that the last insert statement run on your connection or the last insert run on the database for all connections? I guess what I am trying to ask is: on…
Brook Julias
  • 2,007
  • 8
  • 29
  • 43
12
votes
1 answer

can SQL insert using select return multiple identities?

I am insertnig into a table using a selection INSERT california_authors (au_id, au_lname, au_fname) SELECT au_id, au_lname, au_fname FROM authors WHERE State = 'CA' say i have an identity column in california_authors. Can i get all the ids inserted…
Umer
  • 1,721
  • 3
  • 30
  • 42
12
votes
5 answers

SELECT last id, without INSERT

I'm trying to retrieve the id of one table A to insert into another table B. I cannot use last_insert_id() as i have not inserted anything into A. Any ideas on how to do this nicely? $n = mysql_query("SELECT max(id) FROM tablename"); doesn't seem to…
Illes Peter
  • 1,525
  • 4
  • 22
  • 41
1
2 3
8 9