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.
To select last two rows, use ORDER BY DESC LIMIT 2.
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.
To select the last row, we can use ORDER BY clause with desc (descending) property and Limit 1.
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 .
SELECT * FROM Categories ORDER BY CategoryID DESC LIMIT 1,1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With