Re: Complex query
Posted by: Peter Brawley
Date: October 23, 2007 02:02PM

If I understand you correctly ...
(i) form a derived table with the ids, names and counts of the 5 users with the most products,
(ii) join that result with products on userid,
(iii) scope on products.special,
so it would be something like...
SELECT up.user_id, u.name, GROUP_CONCAT(p.product_name)
FROM (
  SELECT user_id,COUNT(product_id) AS N
  FROM users
  JOIN products USING (user_id)
  GROUP BY user_id
  ORDER BY N DESC LIMIT 5
) AS up
JOIN users u USING (user_id)
JOIN products p USING (user_id)
WHERE p.product_special
GROUP BY up.user_id
ORDER BY up.N DESC;
PB

Options: ReplyQuote


Subject
Written By
Posted
October 23, 2007 07:11AM
Re: Complex query
October 23, 2007 02:02PM


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.