Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the last record before the last one in mysql?

How can I get record 34 of a table if the last record is record 35 ?

like image 416
Mulisa Thierry Avatar asked Dec 18 '12 03:12

Mulisa Thierry


People also ask

How will you select a 2nd last row in SQL?

Here is the query to get the second last row of a table in MySQL. mysql> select *from secondLastDemo order by StudentId DESC LIMIT 1,1; The output displays the second last record.

How do I find previous and next records in SQL?

You can use UNION to get the previous and next record in MySQL. Insert some records in the table using insert command. Display all records from the table using select statement.

How can I get recent data in MySQL?

Use the aggregate MAX(signin) grouped by id. This will list the most recent signin for each id . To get the whole single record, perform an INNER JOIN against a subquery which returns only the MAX(signin) per id. Save this answer.

How do I get the last inserted record in SQL?

If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL.


1 Answers

Simplest method

SELECT * FROM tab ORDER BY col DESC LIMIT 1,1

This will pick one record starting with the 2nd LIMIT 1,1 means skip first and pick next

The order by will have to be done so that last is first The col mentioned will most probably be id

If you know the number of the record you want however why can't you just select where id=34?

like image 138
cjds Avatar answered Oct 07 '22 19:10

cjds