Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 November MySQL: Get ID of the Last Updated Row

MySQL: Get ID of the Last Updated Row

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a demonstration on how to get last updated ID of the last updated row in MySQL.

Many times, we require updating the data basis on last updated table id.

We should write update query such a way that we can get a last updated ID in the update statement only.

Here, I am sharing different ways for getting a last updated ID of the last updated row.

Let’s first create a table with sample data.

1
2
3
4
5
6
7
8
9
10
CREATE TABLE tbl_LastUpdateID
(
Rno INTEGER AUTO_INCREMENT
,Name VARCHAR(50)
,CONSTRAINT pk_tbl_LastUpdateID_Rno PRIMARY KEY(Rno)
);
 
INSERT INTO tbl_LastUpdateID (Name) VALUES ('Anvesh');
INSERT INTO tbl_LastUpdateID (Name) VALUES ('Neevan');
INSERT INTO tbl_LastUpdateID (Name) VALUES ('Roy');

First script to get a last updated ID:

1
2
3
4
5
SET @LastUpdateID := 0;
UPDATE tbl_LastUpdateID
SET Name = 'Alex',Rno = (SELECT @LastUpdateID := Rno)
WHERE Name = 'Roy';
SELECT @LastUpdateID AS LastUpdateID;

Get all last updated ID when more than one row updates:

1
2
3
4
5
6
SET @LastUpdateID := NULL;
UPDATE tbl_LastUpdateID
SET Name = 'All'
WHERE Name <>'All'
AND (SELECT @LastUpdateID := CONCAT_WS(',', Rno, @LastUpdateID));
SELECT @LastUpdateID;

Second script using LAST_INSERT_ID():
Before this, please execute the insert script again.

1
2
3
4
5
UPDATE tbl_LastUpdateID
SET Name = 'Martin'
,Rno=LAST_INSERT_ID(Rno)
WHERE Name = 'Roy';
SELECT LAST_INSERT_ID();
Nov 1, 2015Anvesh Patel
MySQL: Error Code-1175 You are using safe update modePostgreSQL: Script to Kill all Running Connections and Sessions of a Database
Comments: 2
  1. desmond
    November 6, 2015 at 4:14 am

    Thanks Anvesh, You helped me lot. I am following you and interested in your articles and collections of DBA script.

  2. Po-sung
    September 8, 2017 at 2:31 am

    Thanks Anvesh! The part of “Get all last updated ID when more than one row updates” helped me a lot! Wondering while you make use of the script, have you encounter an issue that the row actually not updated, but get @LastUpdateID? If so how you fix it? Thank you.

Anvesh Patel
Anvesh Patel

Database Engineer

November 1, 2015 MySQLAnvesh Patel, database, database research and development, dbrnd, LAST_INSERT_ID, MySQL, MySQL Command, MySQL Database Administrator, MySQL Database Designing, MySQL Database Programming, MySQL Error, MySQL Performance Tunning, MySQL Query, MySQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....