0

My select looks like this and it returns the fields from the biggest id...

SELECT * FROM Pontos WHERE IdPonto = (SELECT MAX(IdPonto) FROM Pontos) 

But now I want to select just the fields from the second biggest.

ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
LeandroC
  • 131
  • 3
  • 13

5 Answers5

2

SELECT * FROM Pontos WHERE IdPonto = (SELECT IdPonto FROM Pontos ORDER BY IdPonto DESC LIMIT 1,1) should work.

Christian
  • 4,596
  • 1
  • 26
  • 33
  • Works... But just SELECT * FROM Pontos ORDER BY IdPonto DESC LIMIT 1,1 has the same effect... but i reached to it thanks your help – LeandroC Aug 02 '13 at 16:31
2

This would be one way to do it:

SELECT * FROM Pontos ORDER BY Id DESC LIMIT 1 OFFSET 1

(Not quite certain about sqlite syntax, but read this SO thread for more info: Sqlite LIMIT / OFFSET query)

edit: I do not think it is necessary in this case to run nested queries.

Community
  • 1
  • 1
keaton_fu
  • 444
  • 2
  • 9
0

This is written in standard SQL (it doesn't use LIMIT and OFFSET) and should work:

SELECT * FROM Pontos WHERE IdPontos = (
    SELECT MAX(t1.IdPontos) FROM Pontos t1
    WHERE t1.IdPontos not in (SELECT MAX(IdPontos) FROM Pontos)
)
ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
0

I seems to do the job in standard SQL and in SQLite.

select * from Pontos where IdPonto < (select max(IdPonto) from Pontos) and IdPonto = (select max(IdPonto) from Pontos where IdPonto < (select max(IdPonto) from Pontos));

Hope it helps.

Peter Birdsall
  • 3,159
  • 5
  • 31
  • 48
0
SELECT * FROM Pontos ORDER BY IdPonto DESC LIMIT 1,1
LeandroC
  • 131
  • 3
  • 13