Reputation: 1313
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
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
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
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