selecting the last and second last id's
My primary id is an auto incrementing table and I want to return the highest and second highest id. Any ideas?
Re: selecting the last and second last id's
At 04:00 PM 5/28/2007, [EMAIL PROTECTED] wrote: My primary id is an auto incrementing table and I want to return the highest and second highest id. Any ideas? You should of course have an index on Rcd_Id: Select rcd_id from mytable order by rcd_id desc limit 2 mike -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: selecting the last and second last id's
Hi! [EMAIL PROTECTED] wrote: My primary id is an auto incrementing table and I want to return the highest and second highest id. Any ideas? The simplest answer I can think of is SELECT id FROM tbl ORDER BY id DESC LIMIT 2; This general problem, and its variations, has many solutions, which you can read about here if you are interested: http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ Cheers Baron -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]