Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8107739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:52:32+00:00 2026-06-06T00:52:32+00:00

I am using mysql and facing some problem. I want to retrieve last row

  • 0

I am using mysql and facing some problem. I want to retrieve last row that is inserted.

<< Below are details >>

Below is how I created table.

create table maxID (myID varchar(4))

I inserted four values in it as below

insert into maxID values ('A001')
insert into maxID values ('A002')
insert into maxID values ('A004')
insert into maxID values ('A003')

When I execute select myID, last_insert_id() as NewID from maxID, I get output as below

myId NewID
A001   0  
A002   0  
A004   0  
A003   0    

When I tried below code,

select myId, last_insert_id() as NewID, @rowid:=@rowid+1 as myrow from maxID, (SELECT @rowid:=0) as init

I get output as below.

myId NewID  rowid
A001   0      1
A002   0      2
A004   0      3
A003   0      4

However when I use code select myId, last_insert_id() as NewID, @rowid:=@rowid+1 as myrow from maxID, (SELECT @rowid:=0) as init where @rowid = 4, I get error as Uknown column 'myrow' in where clause

When I use where @rowid=4, I don’t get any data in tables.

Link to play with data

Note: Here I am using 4 just to get desired output. Later I can get this from a query (select max(rowid) from maxID)

Please suggest me what need to do if I want to see only last record i.e. A003.

Thanks for your time.

Update:

I already have millions of data in my table so I can’t add new column in it as suggested below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:52:33+00:00Added an answer on June 6, 2026 at 12:52 am

    It seems that a SELECT is not guaranteed to return rows in any specific order (without using an ORDER BY clause, of course).

    As per the SQL-92 standard (p. 373):

    If an < order by clause > is not specified, then the table specified by the < cursor specification > is T and the ordering of rows in T is implementation-dependent.

    Okay, MySQL is not fully SQL-92-compliant, but this is a serious hint.

    Laurynas Biveinis (apparently affiliated with Percona) also states:

    The order of the rows in the absence of ORDER BY clause (…) could be different due to the moon phase and that is OK.

    The MySQL manual says about InnoDB:

    InnoDB always orders table rows according to [a PRIMARY KEY or NOT NULL UNIQUE index] if one is present.

    As far as I am concerned, I assume MySQL could also reorder rows after an OPTIMIZE TABLE or even reuse empty spaces after many deletes and inserts (I have tried to find an example of this, and have failed so far).

    Given your table structure, the bottomline is, unfortunately, that so many factors could have altered the order of the rows; I see no solution to reliably determine the order they were inserted. Unless you kept all binary logs since you created the table, of course 😉

    Nevertheless, you may still want to add a sequence column to your table. Existing rows would be assigned a possibly inaccurate sequence number, but at least future rows will be correctly sequenced.

    ALTER TABLE maxID ADD sequence INT DEFAULT NULL;
    ALTER TABLE maxID ADD INDEX(sequence);
    ALTER TABLE maxID MODIFY sequence INT AUTO_INCREMENT;
    

    http://sqlfiddle.com/#!2/63a8d/1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using MySQL 5, and my database experienced some corruption last week. The technicians
I am facing some problem with MySQL database. actually It was fast when I
I am facing a strange problem while creating a file in java using MYSQL.
We have been facing a small problem in MySQL procedure. We have placed some
Im trying to copy data to clipboard using zclip jquery. problem that im facing
I'm facing a problem with a slider that I'm using on a website. (
(Using MySQL and PHP) I have a search form that will allow my users
We're using MySQL with Innodb Engine storage. We have an evented environment that sends
i'm trying to run Redmine using rubyEE, but I am constantly facing some action_controller
I'm facing some issues with a rapidly growing table at increasing speed (currently 4mio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.