How to get a second maximum value?

A

Anonymous

Guest
How to get a id of the second maximum value in mysql?
reply asap

Thank you
 
not exactly sure what you mean, only thing i can think of is that you mean the second to last id in a database...

Code:
$row = mysql_fetch_array(mysql_query("SELECT `id` FROM `database_here` ORDER BY `id` DESC LIMIT 1, 2"));

if you dont mean that then please explain what you do mean
 
I know this post is over a month old, but anyways...

if you use MySQL 4.1:
SELECT MAX(id) FROM table WHERE id<(SELECT MAX(id) FROM table);

or something like that...
 
tranquillo said:
I know this post is over a month old, but anyways...

if you use MySQL 4.1:
SELECT MAX(id) FROM table WHERE id<(SELECT MAX(id) FROM table);

or something like that...

Ouch.. GostLy's solution ought to work just fine -- doing a sub-select for something this simple is a huge waste of CPU cycles.
 
Back
Top