Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting penultimate record from a table

Tags:

sql

mysql

I have the following table:

enter image description here

I need to select penultimate record matching some probID and studentID. How should I do that?

like image 671
user5718409 Avatar asked Dec 28 '25 20:12

user5718409


1 Answers

SELECT * FROM tbl
    WHERE (probID = 2) AND (studentID = 2)
    ORDER BY id DESC
    LIMIT 1 OFFSET 1

PS: This will not return any result for (probID, studentID) = (2,13), because no penultimate record exists for this combination. If this is not desired, you will need to specify what should happen in this case.

like image 138
Guido Avatar answered Dec 31 '25 12:12

Guido