How do I get all the Ids from my last insert in Mysql?
-
Hi, I am using MySQL. Can someone tell me how to write the sql query for retrieving all the last inserted Ids (plural) ? I have a table in which a column has been inserted by serveral rows everytime depending on how many subject the users tick in the checkboxes. Now, I have to get all the auto-incremental Ids from all the Ids that have just been inserted. Is there a way to do it via sql query ?
-
Hi, I am using MySQL. Can someone tell me how to write the sql query for retrieving all the last inserted Ids (plural) ? I have a table in which a column has been inserted by serveral rows everytime depending on how many subject the users tick in the checkboxes. Now, I have to get all the auto-incremental Ids from all the Ids that have just been inserted. Is there a way to do it via sql query ?
-
If you know the number of rows that you want: Mysql: Select top N max values? - Stack Overflow[^].
-
Hi, I am using MySQL. Can someone tell me how to write the sql query for retrieving all the last inserted Ids (plural) ? I have a table in which a column has been inserted by serveral rows everytime depending on how many subject the users tick in the checkboxes. Now, I have to get all the auto-incremental Ids from all the Ids that have just been inserted. Is there a way to do it via sql query ?
In MS SQL Server, you'd use the
OUTPUT
clause. Unfortunately, there doesn't seem to be an equivalent for MySQL. Selecting the top N rows won't work - another user might have inserted rows between yourINSERT
andSELECT
statements. That's why theLAST_INSERT_ID
function exists. MySQL :: MySQL 5.7 Reference Manual :: 27.8.15.3 How to Get the Unique ID for the Last Inserted Row[^] I suspect you might need to insert the values one at a time, and useLAST_INSERT_ID
to retrieve the ID.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
In MS SQL Server, you'd use the
OUTPUT
clause. Unfortunately, there doesn't seem to be an equivalent for MySQL. Selecting the top N rows won't work - another user might have inserted rows between yourINSERT
andSELECT
statements. That's why theLAST_INSERT_ID
function exists. MySQL :: MySQL 5.7 Reference Manual :: 27.8.15.3 How to Get the Unique ID for the Last Inserted Row[^] I suspect you might need to insert the values one at a time, and useLAST_INSERT_ID
to retrieve the ID.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Hi Richard, I won't be able to know because each time the subject_Ids varies, it is dependent on the user's no of checkboxes that have been ticked. So, how ?
-
Hi, I am using MySQL. Can someone tell me how to write the sql query for retrieving all the last inserted Ids (plural) ? I have a table in which a column has been inserted by serveral rows everytime depending on how many subject the users tick in the checkboxes. Now, I have to get all the auto-incremental Ids from all the Ids that have just been inserted. Is there a way to do it via sql query ?
You can add a timestamp or guid column to mark them. Essentially, you need to have your own logic.
There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.