473,413 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Select Last_insert_id query...

jim
Hi,

I have a site with various parts which allow users to enter info via forms
which gets entered into various tables in a MySQL db (I'm using ASP, rather
than PHP).

I have an ecards bit, and when users send a card, their data is entered into
the db via something like:

INSERT vars into ecard_tbl....

I just read that I can use this to get the most recent id from the
autoincrement field in the table:

SELECT LAST_INSERT_ID

However, it doesn't seem to need a table name for that. What if exactly at
the same time another users enters something into my guestbook. That will
submit data to the same MySQL db, so won't the 'SELECT LAST_INSERT_ID' give
me that ID, and not the ID from the card submission?

Am I missing something very obvious here? I checked the MySQL docs but can't
see anything obvious.

Thanks

Jim
Jul 20 '05 #1
6 12660
jim wrote:
However, it doesn't seem to need a table name for that. What if exactly at
the same time another users enters something into my guestbook. That will
submit data to the same MySQL db, so won't the 'SELECT LAST_INSERT_ID' give
me that ID, and not the ID from the card submission?

Am I missing something very obvious here? I checked the MySQL docs but can't
see anything obvious.


"The value of mysql_insert_id() is affected only by statements issued
within the current client connection. It is not affected by statements
issued by other clients."

http://dev.mysql.com/doc/mysql/en/mysql_insert_id.html
Jul 20 '05 #2

"Aggro" <sp**********@yahoo.com> wrote in message
news:fT**************@read3.inet.fi...
jim wrote:
However, it doesn't seem to need a table name for that. What if exactly at the same time another users enters something into my guestbook. That will submit data to the same MySQL db, so won't the 'SELECT LAST_INSERT_ID' give me that ID, and not the ID from the card submission?

Am I missing something very obvious here? I checked the MySQL docs but can't see anything obvious.


"The value of mysql_insert_id() is affected only by statements issued
within the current client connection. It is not affected by statements
issued by other clients."

http://dev.mysql.com/doc/mysql/en/mysql_insert_id.html


This is why you might want to SELECT max(ID) FROM table to get the lastest
one.

Rich
Jul 20 '05 #3
Rich R wrote:
This is why you might want to SELECT max(ID) FROM table to get the lastest
one.


I interpret jim's issue that he wants to fetch the last insert ID within
the context of his current connection, and ignore potential other ID's
inserted within other connections.

The LAST_INSERT_ID() function works correctly in this case, because it
is limited to the scope of the current connection. The function returns
the same value when you call it soon after your last insert or a long
time after your last insert, even if other connections insert more
records in the meantime.

Regards,
Bill K.
Jul 20 '05 #4

"Bill Karwin" <bi**@karwin.com> wrote in message
news:cq*********@enews2.newsguy.com...
Rich R wrote:
This is why you might want to SELECT max(ID) FROM table to get the lastest one.


I interpret jim's issue that he wants to fetch the last insert ID within
the context of his current connection, and ignore potential other ID's
inserted within other connections.

The LAST_INSERT_ID() function works correctly in this case, because it
is limited to the scope of the current connection. The function returns
the same value when you call it soon after your last insert or a long
time after your last insert, even if other connections insert more
records in the meantime.

Regards,
Bill K.


Then maybe I am mistaken, too. I created two tables each having an
auto_increment columm. I did an insert into the first and LAST_INSERT_ID() =
1. I do an insert into the second table and the LAST_INSERT_ID() = 1. I do
another insert into the first table and LAST_INSERT_ID() = 2. So I've lost
the last inserted value from the second table, which is still 1. These
inserts were done on the same connection, one right after another. . So the
only way I can get the last inserted value from the second table is to do a
max() on the column. Am I out in the weeds on this one?

Best regards,
Rich
Jul 20 '05 #5
Rich R wrote:
Then maybe I am mistaken, too. I created two tables each having an
auto_increment columm. I did an insert into the first and LAST_INSERT_ID() =
1. I do an insert into the second table and the LAST_INSERT_ID() = 1. I do
another insert into the first table and LAST_INSERT_ID() = 2. So I've lost
the last inserted value from the second table, which is still 1. These
inserts were done on the same connection, one right after another. . So the
only way I can get the last inserted value from the second table is to do a
max() on the column. Am I out in the weeds on this one?


Yes, that is the way LAST_INSERT_ID() functions. It returns the last ID
that was auto-generated for any table within the current database
connection. If you do another insert to a table (either to the same
table or to a different table), then LAST_INSERT_ID() doesn't return the
previously generated ID value, it returns the more recent one.

However, that is not the issue that the original poster was asking
about. He was asking about the case where a second connection to the
database does its own insert, and causes another ID to be generated.
LAST_INSERT_ID() returns different values in this case, regardless of
the sequence of inserts. In each connection, the function returns the
most recent ID auto-generated by an insert within that respective
connection.

LAST_INSERT_ID() handles the case that jim was posting about. Your two
posts so far have described different issues, unrelated to jim's posting.

Regards,
Bill K.
Jul 20 '05 #6

"Bill Karwin" <bi**@karwin.com> wrote in message
news:cq*********@enews4.newsguy.com...
Rich R wrote:
Then maybe I am mistaken, too. I created two tables each having an
auto_increment columm. I did an insert into the first and LAST_INSERT_ID() = 1. I do an insert into the second table and the LAST_INSERT_ID() = 1. I do another insert into the first table and LAST_INSERT_ID() = 2. So I've lost the last inserted value from the second table, which is still 1. These
inserts were done on the same connection, one right after another. . So the only way I can get the last inserted value from the second table is to do a max() on the column. Am I out in the weeds on this one?


Yes, that is the way LAST_INSERT_ID() functions. It returns the last ID
that was auto-generated for any table within the current database
connection. If you do another insert to a table (either to the same
table or to a different table), then LAST_INSERT_ID() doesn't return the
previously generated ID value, it returns the more recent one.

However, that is not the issue that the original poster was asking
about. He was asking about the case where a second connection to the
database does its own insert, and causes another ID to be generated.
LAST_INSERT_ID() returns different values in this case, regardless of
the sequence of inserts. In each connection, the function returns the
most recent ID auto-generated by an insert within that respective
connection.

LAST_INSERT_ID() handles the case that jim was posting about. Your two
posts so far have described different issues, unrelated to jim's posting.

Regards,
Bill K.


Hi Bill,

I think I addressed the post correctly. Doesn't matter if it's one
connection or many. If you want the last ID assigned you must do a Max(ID).
Within 1 connection, auto_increment will change not based on tables, simply
adding 1 to the last one. So if I want the last auto_incremented EmployeeID
and I have done some inserts to Projects with an ID ->auto_increment, all
bets are off, even safer, there are no bets.

If the OP did this all inside a transaction with InnoDB, then I still
wouldn't trust it.

Regards,

Rich

Regards,
Rich
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
$sql = 'INSERT INTO fs_usermetadata (' . substr(trim($cols), 0, strrpos(trim($cols), ',')) . ') VALUES (' . substr(trim($values), 0, strrpos(trim($values), ',')) . ')'; if (!mysql_query($sql))...
17
by: guitarromantic | last post by:
Hey everyone. I'm re-writing a php Content Management System (based loosely on phpNuke but specific to my site). My first major change was attempting to normalise the Review section database...
5
by: Terri | last post by:
The following query will give me the top ten states with the most callers. SELECT TOP 10 Count(Callers.CallerID) AS CountOfCallerID, Callers.State FROM Callers GROUP BY Callers.State ORDER BY...
3
by: | last post by:
Is a SELECT TOP query allowed in filtering rows from a datatable? If it is not, is there any other easy way to filter a given number of rows from a datatable? Thanks a lot for your help.
3
lwwhite
by: lwwhite | last post by:
I've got a a datasheet subform that gets its data from a SELECT DISTINCT query. The data is not editable. When I remove the DISTINCT qualifier from the query, the data is editable, but of course I...
2
by: wizardry | last post by:
Hello - I'm recently a newbie to mysql, but have experiance in oracle, mssql, access, etc... Here is my problem, i've created the table needed to test the audit procedures. the data element...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.