LAST_INSERT_ID() MySQL
You could store the last insert id in a variable :
INSERT INTO table1 (title,userid) VALUES ('test', 1);
SET @last_id_in_table1 = LAST_INSERT_ID();
INSERT INTO table2 (parentid,otherid,userid) VALUES (@last_id_in_table1, 4, 1);
Or get the max id from table1 (EDIT: Warning. See note in comments from Rob Starling about possible errors from race conditions when using the max id)
INSERT INTO table1 (title,userid) VALUES ('test', 1);
INSERT INTO table2 (parentid,otherid,userid) VALUES (LAST_INSERT_ID(), 4, 1);
SELECT MAX(id) FROM table1;
(Warning: as Rob Starling points out in the comments)
Get the last inserted row ID (with SQL statement)
If your SQL Server table has a column of type INT IDENTITY
(or BIGINT IDENTITY
), then you can get the latest inserted value using:
INSERT INTO dbo.YourTable(columns....)
VALUES(..........)
SELECT SCOPE_IDENTITY()
This works as long as you haven't inserted another row - it just returns the last IDENTITY
value handed out in this scope here.
There are at least two more options - @@IDENTITY
and IDENT_CURRENT
- read more about how they works and in what way they're different (and might give you unexpected results) in this excellent blog post by Pinal Dave here.
Select last insert id
Check out mysql_insert_id()
mysql_query($sql);
$id = mysql_insert_id();
When that function is run after you've executed your INSERT statement in a mysql_query() command its result will be the ID of the row that was just created.
Get the Last Inserted Id Using Laravel Eloquent
After save, $data->id
should be the last id inserted.
$data->save();
$data->id;
Can be used like this.
return Response::json(array('success' => true, 'last_insert_id' => $data->id), 200);
For updated laravel version try this
return response()->json(array('success' => true, 'last_insert_id' => $data->id), 200);
How to select last insert id without collision
No, last_insert_id is maintained on a per-connection basis, so if two connections are running at the same time, one won't get the other's inserted value.
How to get last id inserted. mysql_insert_id() not working
You need to use this in OpenCart for getting last insert id:
$this->db->getLastId()
From OpenCart User Guide:
Method Reference / DB::getLastId(): Returns the ID generated for an AUTO_INCREMENT
column by the previous query.
Getting last insert id from MySQL stored in Python
cursor.lastrowid
is set from mysql_insert_id()
instead of LAST_INSERT_ID()
.
You can manually set it from LAST_INSERT_ID()
:
cursor.callproc(procedure_name, params_list)
cursor.execute('SELECT LAST_INSERT_ID()')) # Add this
cursor.lastrowid = cursor.fetchone()[0] # Add this
print('last id:', cursor.lastrowid)
From https://dev.mysql.com/doc/c-api/8.0/en/mysql-insert-id.html:
mysql_insert_id()
returns0
following aCALL
statement for a stored procedure that generates anAUTO_INCREMENT
value because in this casemysql_insert_id()
applies toCALL
and not the statement within the procedure. Within the procedure, you can useLAST_INSERT_ID()
at the SQL level to obtain theAUTO_INCREMENT
value.The reason for the differences between
LAST_INSERT_ID()
andmysql_insert_id()
is thatLAST_INSERT_ID()
is made easy to use in scripts whilemysql_insert_id()
tries to provide more exact information about what happens to theAUTO_INCREMENT
column.
How efficient is Last_insert_id?
insert_last_id
returns the last inserted ID of the very same SQL session/connection (see MySQL manual). - The SQL server does not know about your pages. As long as the connection/session is not shared there are no problems (such as race conditions).
It's also possible to get the last inserted ID using SQL only: select LAST_INSERT_ID();
or use it directly in a following SQL query in order to reduce client-server round-trips like insert table B set foreign_id=LAST_INSERT_ID(),data="something";
(note, however, if the following SQL statement also has an auto increment column the next LAST_INSERT_ID()
call would return the newest inserted ID; see MySQL manual)
Related Topics
Pdo Mssql Server - Driver Not Found
Find a Percentage Value in a String Using Preg_Match
Fatal Error: Call to Undefined Function MySQL_Connect() in C:\Apache\Htdocs\Test.PHP on Line 2
How to Curry Method Calls in PHP
Nohup on Windows, Exec Without Waiting for Finish
PHP Dynamic Db Page Rewrite Url
Codeigniter Command Line Error - PHP Fatal Error: Class 'Ci_Controller' Not Found
Expected Response Code 250 But Got Code "", with Message ""
Model->Save() Not Working in Yii2
Filter_Sanitize VS Filter Validate, Whats the Difference - and Which to Use
In Laravel 5, How to Disable Verifycsrftoken Middleware for Specific Route
Can't Install Xdebug on Windows
PHP MySQL Set Connection Timeout
Zend Framework 2 - Removed Form Element Causes Validation to Fail