Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get second last row from a mysql database?

Tags:

sql

mysql

I just made a query to get second last row from database:

Here Is code:

SELECT TOP 1 * FROM
   (select Top 2 * from Categories ORDER BY CategoryID DESC)x                     
   ORDER BY CategoryID

but this code gives me error syntax error in new version.

In older version this code work fine.

What is problem: I have 10 results in the table, and when I submit this query, it gives me 9 row. But when 9 row delete from table, now I have 8 and 10. But this code gives me 10.

like image 310
Hassan Avatar asked Feb 11 '15 09:02

Hassan


People also ask

How do I select the last two rows in SQL?

To select last two rows, use ORDER BY DESC LIMIT 2.

How do I select the second row in SQL?

For SQL Server, a generic way to go by row number is as such: SET ROWCOUNT @row --@row = the row number you wish to work on.

How do I get the last row in MySQL?

To select the last row, we can use ORDER BY clause with desc (descending) property and Limit 1.

How do I get the last 3 rows of a SQL table?

I want to select the last 3 rows of an sql table. I know I should use SELECT * FROM table ORDER BY DESC LIMIT 3 , but the problem with this code is that it selects the rows from the end. For example, it selects 30, then 29, then 28. But, I need them in this format: 28, 29, 30 .


1 Answers

SELECT * FROM Categories ORDER BY CategoryID  DESC LIMIT 1,1
like image 131
Shailesh Katarmal Avatar answered Oct 11 '22 21:10

Shailesh Katarmal