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
1
vote
1 answer

last_insert_id in Powershell

In Powershell, I have a simple Mysql insert command. $query = "INSERT INTO TABLE1 (id, col1, col2) VALUES (1, 'two', 'three');"
$command = New-Object MySql.Data.MySqlClient.MySqlCommand($query, $connection) $command.ExecuteNonQuery() I'm using…
steve
  • 849
  • 2
  • 9
  • 15
1
vote
4 answers

Magento last insert id in middle of transaction

I am using transactions in Magento. I need to use primeryKey of first insert query to all my subsequent queries. $model1->setfield1() ->setField2(); $transaction->addObject($model1); $connection =…
sushantsahay
  • 361
  • 2
  • 7
  • 15
1
vote
2 answers

Insert n rows with LAST_INSERT_ID

I have 3 tables called POSTS, HASHTAGS and POSTS_HASHTAGS_RELATION, as below. CREATE TABLE POSTS( post_id int unsigned NOT NULL AUTO_INCREMENT, content varchar(200) NOT NULL, PRIMARY KEY (post_id) ); CREATE TABLE HASHTAGS( hashtag_id int…
longhorn
  • 15
  • 4
1
vote
4 answers

I cant get lastInsertId on laravel 7 project

$sql = DB::table('laravel_products') ->insert(array( 'name' => $name, 'price' => $price, 'qty' => $qty, 'description' => $description, 'uruu' => $uruu, 'garage' => $garage, 'duureg' =>…
1
vote
1 answer

PHP PDO MySql lastInsertId() returning 0

I have the following code which i have extracted and simplified from the site. This code works fine with just a few fields But i am unable to get lastInsertId() working, it always returns a 0 Please help
1
vote
0 answers

Does PDO::lastInserteId() return last inserted id from a specific table?

Does PDO::lastInsertId() returns last inserted id among all the tables in database? or from a specific table currently it working on? I have a doubt like if multiple users working on website and they try to execute some same kind of a operation and…
Kasun
  • 672
  • 8
  • 18
1
vote
1 answer

How to get last inserted id in FluentMigrator

I'm using FluentMigrator with MySql and C#. [Migration(3)] public class _0003_TestMigrationTI : Migration { public override void Up() { Insert.IntoTable("cmn_file_types").Row(new { Name = "Image", Code = "IMG" }); ??? …
nameless
  • 11
  • 2
1
vote
1 answer

I can't see continuously the last insert data from my MySQL database to my html page

I use flask framework and with this code i get the last insert data,but i want to display continuously the last insert. When i reboot the flask framework i get it.Any help? app.py @app.route("/sensor" , methods=['POST','GET']) def sensor(): …
1
vote
1 answer

Immediately grab auto-generated field in mySql for use

During this fetch, it creates a new record, and auto increments a field called "employee_id" in mySql table. That part works fine. But now what I want to add to this is, to immediately, within this same function if possible, setState with that…
Adam Norton
  • 512
  • 2
  • 5
  • 21
1
vote
2 answers

How do I insert into a datetime column minus the seconds?

So I'm working on a c# project that creates row in a table with a datetime column and then extract that auto generated ID of that column afterwards. My way of extracting the ID is through the code below the problem is the length it takes to execute…
Jam
  • 117
  • 11
1
vote
1 answer

How to use lastInsertId in this case?

What I want is to get and store the last inserted ID, but I am not sure how. The scenario is, after a user add a record he/she will be redirected to a page that will show him/her of the summary of what he/she saved. But I am not sure how I can do…
aaa28
  • 81
  • 8
1
vote
1 answer

Is PDO::lastInsertId() in multithread single connection safe?

I read some threads here about PDO::lastInsertId() and its safety. It returns last inserted ID from current connection (so it's safe for multiuser app while there is only one connection per user/script run). I'm just wondering if there is a…
TSGR
  • 35
  • 2
1
vote
2 answers

How to fetch last inserted record in mysql using nifi

am trying to insert a record in mysql database and fetch the last inserted record on success of insertion using nifi. below is flow structure am trying. convertJsontomysql -> putSQL(insert record) -> executeSQL (query to fetch last inserted…
sakthivel
  • 71
  • 2
  • 10
1
vote
2 answers

MySQL: Get last inserted primary key without auto_increment

I removed my record ID while I'm using unique hashes as a primpary key. This primary key obviously cannot auto increment. Now my question is how to retreive the last inserted primary key? MySQL returns 0 on LAST_INSERT_ID() while it's not an auto…
codekandis
  • 712
  • 1
  • 11
  • 22
1
vote
1 answer

Is last insert id method reliable

The following scenario. We have 2 scripts, both inserting into table and then using lastInsertId() to get the value from an autoincrement column. If these 2 scripts are executed in parallel, do we know for sure that they won't mess the results? In…
DonJoe
  • 1,603
  • 1
  • 13
  • 24