Page 1 of 1

Select top 1 and top 2-6 records from MySQL

Posted: Sat Jul 05, 2008 3:16 pm
by lyleyboy
Hi all,

I am in need of two queries from the same table. I have an ID which is an auto incremented number. There will be some gaps in the ID range.
Query 1,
I want to pull the record with the highest ID
i.e. I have records 1,3,4,6,7,9,11,15,18,20,23 I only want to pull ID 23

Query 2,
I want to pull the next five records
i.e. 23,20,18,15 and 11

I hope this makes sense to someone.

Thanks in advance

Re: Select top 1 and top 2-6 records from MySQL

Posted: Sat Jul 05, 2008 3:38 pm
by jaoudestudios
Makes sense :)

Query 1
SELECT * FROM *TABLE* ORDER BY id DESC LIMIT 0,1

Query 2
SELECT * FROM *TABLE* ORDER BY id DESC LIMIT 0,5

Replace *TABLE* with the name of the table in your database

Re: Select top 1 and top 2-6 records from MySQL

Posted: Sat Jul 05, 2008 4:29 pm
by lyleyboy
Superb thanks for that. I knew there would be an easy way
jaoudestudios wrote:Replace *TABLE* with the name of the table in your database
I'm not that daft :D

Re: Select top 1 and top 2-6 records from MySQL

Posted: Tue Jul 08, 2008 1:16 pm
by Ollie Saunders
I'm not that daft :D
Hehe, well that's good but some people are.