MySQL Forums
Forum List  »  General

Re: combine this 2 simple querys into 1 query for performance.
Posted by: Rick James
Date: September 14, 2011 09:01PM

Can there be gaps in the ids?
If not, you can find the MAX(id), do some arithmetic, and a UNION:
SELECT @max := MAX(id) FROM tbl;
SELECT ... WHERE id >= @start ORDER BY id
UNION ALL
SELECT ... WHERE id < @start + @count - @max ORDER BY id

Whether or not there are gaps, this _might_ work:
(
SELECT ... WHERE id >= @start ORDER BY id
UNION ALL
SELECT ... WHERE ORDER BY id
) LIMIT 5
Added bonus: It should work even if it does not wrap.

Options: ReplyQuote


Subject
Written By
Posted
Re: combine this 2 simple querys into 1 query for performance.
September 14, 2011 09:01PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.