Redone
Redone

Reputation: 1313

Get the last result from the mysql resultset in mysql stored procedure

I have a table with data as follows.

Name      Age
Alex      23
Tom       24

Now how do i get the last row only ie. the row containing "Tom" in Mysql Stored Procedure using the select statement and without considering the Name and Age. Thanks.

Upvotes: 0

Views: 403

Answers (5)

Navdeep Singh
Navdeep Singh

Reputation: 689

Every idle table should have Auto_increment index named INDEX_NO which use to have highest number, then your table would have items this way

INDEX_NO   Name      Age
 1         Alex      23
 2         Tom       24

and then you can use this query and i can say that this is fast.

SELECT * from table
ORDER BY INDEX_NO desc
LIMIT 1;

Upvotes: 0

fancyPants
fancyPants

Reputation: 51888

without considering the Name and Age

That's not possible. There's no "first" or "last" row in a result set as long as you don't add an ORDER BY a_column to your query. The result you see is kind of random. You might see the same result over and over again, when you execute the query 1000 times, this might change however when an index gets rewritten or more rows are added to the table and therefore the execution plan for the query changes. It's random!

Upvotes: 1

Mark M
Mark M

Reputation: 1600

SELECT * from table
ORDER BY Name desc
LIMIT 1;

Upvotes: 0

waka
waka

Reputation: 3417

SELECT TOP 1 name, age FROM table ORDER BY name desc or use ORDER BY to order it by whatever you want to order your list by. The trick is, to order it descending, instead of ascending.

Upvotes: 0

StanislavL
StanislavL

Reputation: 57391

Add ORDER BY and limit 1 to your select * from...

Upvotes: 0

Related Questions