MySQL Forums
Forum List  »  Newbie

Re: union and missing limits
Posted by: Rick James
Date: April 08, 2010 07:51AM

See if this technique applies in your situation:
(
    ( SELECT ...
        ORDER BY ... LIMIT 10 )
    UNION
    ( SELECT ...
        ORDER BY ... LIMIT 10 )
) ORDER BY ... LIMIT 10
That is, get the LIMIT from each part of the UNION, but pessimistically assume each select providing the desired 10 rows. Then blend the 20 rows together and pick the real 10 you desired.

If you are doing this for "pagination" (and I can rant against 'pagination via LIMIT'), then it gets more complicated for subsequent pages. Page 3, for example:
(
    ( SELECT ...
        ORDER BY ... LIMIT 30 ) -- yes, all 30
    UNION
    ( SELECT ...
        ORDER BY ... LIMIT 30 )
) ORDER BY ... LIMIT 20,10 -- skip 20, then deliver the 3rd page's worth
By the time you are at the last page, it is terribly slow.

As for the syntax error -- please show me. If it says
   ... near 'xxxx'
the xxxx pinpoints where the problem is. If it says
   ... near ''
then the problem is at the end of the statement.

Options: ReplyQuote


Subject
Written By
Posted
M B
March 27, 2010 04:49AM
March 27, 2010 10:35AM
M B
March 27, 2010 11:45AM
March 28, 2010 12:33PM
M B
March 28, 2010 02:53PM
M B
April 08, 2010 01:02AM
M B
April 08, 2010 01:41AM
Re: union and missing limits
April 08, 2010 07:51AM
M B
April 08, 2010 09:11AM
April 09, 2010 12:33AM
M B
April 09, 2010 03:59AM
April 09, 2010 09:42AM
M B
April 09, 2010 01:06PM
April 09, 2010 09:41PM
M B
April 12, 2010 03:48AM
April 08, 2010 07:56AM
April 08, 2010 08:46AM


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.