Select Last Insert Id

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() returns 0 following a CALL statement for a stored procedure that generates an AUTO_INCREMENT value because in this case mysql_insert_id() applies to CALL and not the statement within the procedure. Within the procedure, you can use LAST_INSERT_ID() at the SQL level to obtain the AUTO_INCREMENT value.

The reason for the differences between LAST_INSERT_ID() and mysql_insert_id() is that LAST_INSERT_ID() is made easy to use in scripts while mysql_insert_id() tries to provide more exact information about what happens to the AUTO_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



Leave a reply



Submit