How would I query a 'Top Submitter' kind of thing for MySQL?

Discussion in 'PHP' started by Crayz, Oct 30, 2008.

  1. #1
    Hey! I'm curious as to how I could find the top submitter most efficiently.

    Right now, there aren't any mysql rows that increment each time a user adds a resource. Right now, I'm thinking this would be the best way to go about with it, but there may be a mysql function that I'm missing =[

    Anywho, I have multiple tables: SK_layouts, SK_graphics, SK_articles

    When a user submits a resource, it is placed into the proper mysql table. Basically what I need is a way to find my top submitter (most resources added) of each category, and the top submitter of all three combined.

    Is there an efficient way of doing this? Or would I be best off by adding a 'totalGraphics', 'totalLayouts', etc.. kind of row to my SK_users table?

    Any suggestions?
    Thanks!
     
    Crayz, Oct 30, 2008 IP
  2. ivo.pavlik

    ivo.pavlik Member

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    My suggestion:
    1. Create a view:
    create view hits as
    select 'layouts' as category,user,count(*) as cnt from sk_layouts group by category,user union all
    select 'graphics' as category,user,count(*) as cnt from sk_graphics group by category,user union all
    select 'articles' as category,user,count(*) as cnt from sk_articles group by category,user;
    Code (markup):
    2. Use queries like:
    select user,cnt from hits where category='layouts' and cnt=(select max(cnt) from categories where category='layouts');  
    select user,cnt from hits where category='layouts' order by cnt desc;
    Code (markup):
    (top submitter of all without clause filtering category)

    There is possibility to join the queries into one nested query, but you can have top submitters with same number of hits, what you cannot provide in nested query, or use limit 1. Don't know, if it is the most efficient solution, but it is a solution.
    Other way could be, as you mentioned, store number of hits in table where are users stored and update it with a trigger. Grouping queries takes some time.

    Ivo.
     
    ivo.pavlik, Oct 31, 2008 IP