MySQL Forums
Forum List  »  Newbie

Re: union and missing limits
Posted by: Rick James
Date: April 09, 2010 09:41PM

No, I think I missed something -- the GROUP BY (outer query) invalidates the pattern I presented. Sorry.

Another pattern:

1. Each inner query gathers subtotal, something like
    SELECT user,
           color,
           piece,
           SUM(quantity) AS sum_quantity
        FROM  test1_loose
        GROUP BY user, color, piece
        ORDER BY user, color, piece  -- let's be clearer about order
        LIMIT 0,50
You should explicitly say UNION ALL, else you will lose some values.
2. Then the outer query sums the sums:
SELECT  user, color, piece,
        SUM(sum_quantity) AS quantity  -- summing the sums
   FROM (... UNION ALL ...)   -- note the ALL
   GROUP BY user, color, piece
   ORDER BY user, color, piece  -- suggest you match the GROUP BY
   LIMIT 0,50
I think (hope) this will safely get past your questions.

Puzzle: You are getting the first 50 rows; then what? Pagination? With the possibility of a user split between pages?

If one of the subselects has missing users/colors/pieces, there should be no problem; the first 50 will overlay, and some will be tossed.

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
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
Re: union and missing limits
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.