If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > list products of the top 5 users with the highest number of products (was "Query")

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-23-07, 09:24
muscailie muscailie is offline
Registered User
 
Join Date: Oct 2007
Posts: 1
Question list products of the top 5 users with the highest number of products (was "Query")

Hello, i`m trying since morning to get a result.

I have a users table and a table which holds products for selling..
Each user has the option to mark several products as 'special'.

i need a list which contains the name of the 'special' products which belong to the first 5 users who have the highest number of products.

users(user_id,user_name)
products(product_name,product_special,user_id)

select count(user_id),user_name as no
from users
natural join products
order by no desc
limit 5 -> this query shows the first 5 users which have the most products

select product_name,user_name
natural join users
where product_special='1' -> this query shows all the 'special' products

I need the result of the two queries combined somehow.

thanks!
Reply With Quote
  #2 (permalink)  
Old 10-23-07, 09:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
select products.product_name
     , products.user_id
  from users as T
inner
  join products
    on products.user_id = T.user_id
   and products.product_special = 1
 where (
       select count(*) 
         from (
              select user_id 
                   , count(*) as productcount
                from products
              group 
                  by user_id
              ) as c
        where productcount > (
              select count(*) 
                from products
               where user_id = T.user_id
              ) 
       ) < 5
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On