2013-07-30 64 views
0

我選擇這個樣子的所有領域,並返回從最大的ID字段...SQL:從第二大ID

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

但現在我要選擇剛剛從第二個領域最大。

回答

2

SELECT * FROM Pontos WHERE IdPonto = (SELECT IdPonto FROM Pontos ORDER BY IdPonto DESC LIMIT 1,1)應該工作。

+0

作品......但是,僅僅SELECT * FROM潘託斯ORDER BY IdPonto DESC LIMIT 1,1有同樣的效果...但我達到它感謝您的幫助 – LeandroC

2

這將是做到這一點的一種方法:

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

(不是很有把握SQLite的語法,但閱讀本SO線程的詳細信息:Sqlite LIMIT/OFFSET query

編輯:我不認爲這在這種情況下需要運行嵌套查詢。

0

這是寫在標準SQL(它不使用LIMIT和OFFSET),並應工作:

SELECT * FROM Pontos WHERE IdPontos = (
    SELECT MAX(t1.IdPontos) FROM Pontos t1 
    WHERE t1.IdPontos not in (SELECT MAX(IdPontos) FROM Pontos) 
) 
+0

注:它可以在SQL Server中了。 –

0

我看來,做標準的SQL和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)); 

希望它有幫助。

0
SELECT * FROM Pontos ORDER BY IdPonto DESC LIMIT 1,1