Top 10 list query question?

Discussion in 'MySQL' started by 123GoToAndPlay, Aug 18, 2006.

  1. #1
    Hi,

    I need a little help with my top 10 best rated query. Right now I have:

    $sql = "SELECT * FROM tblPix, tblVoting WHERE tblVoting.picID = tblPix.picID ORDER BY picRating DESC LIMIT 10";

    This one works but not as I want to. I want to take into account the num of votes. The tblVoting has a field called numOfVotes.

    So a rating of 5 with 10 votes will be above a rating of 5 with 6 votes.

    Any suggestions??
     
    123GoToAndPlay, Aug 18, 2006 IP
  2. LinkBliss

    LinkBliss Peon

    Messages:
    697
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You have to set a rule. Maybe a threshold such as nothing with less than 10 votes will be considered?

    Eric
     
    LinkBliss, Aug 18, 2006 IP
  3. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $sql = "SELECT * FROM tblPix, tblVoting WHERE tblVoting.picID = tblPix.picID ORDER BY picRating DESC, numOfVotes DESC LIMIT 10";

    That'll make 5 with 10 votes come before 5 with 6 votes, but won't make 5 with 10 votes come before 6 with 1 vote - you'd have to calculate a weighted value based on number of votes and rating.
     
    void, Aug 18, 2006 IP
  4. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    @void that's the one with the two ORDER BY columns Thanks.

    Must dive into that one.
     
    123GoToAndPlay, Aug 18, 2006 IP
  5. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Have fun with the weighting and report back if you're stuck :)
     
    void, Aug 18, 2006 IP
  6. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #6
    is this the best practice for table joins in MySQL?

    SELECT * FROM tblPix, tblVoting

    I generally do inner or left outer... (Just interested which is better here)
     
    ccoonen, Aug 21, 2006 IP
  7. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Not sure about best practice, but it's what I use. Don't forget to add
    WHERE tblVoting.picID = tblPix.picID
    Code (markup):
    though!
     
    void, Aug 22, 2006 IP
  8. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    @void, I use the same "simple" method as you do. Although I have to read up on the query possibilities of mysql. Like ccoonen points out. I have used it one or twice for a project, but I can't remember why ;)

    Still got to do the weighted issue
     
    123GoToAndPlay, Aug 22, 2006 IP
  9. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #9
    Will require two queries.

    First for count.

    then deduct 10 from the count and then use the limit thing.
     
    Nick_Mayhem, Aug 22, 2006 IP
  10. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #10
    @Nick_Mayhem - do you mean this, or am I missing the point completely?

    $sql = "SELECT * FROM tblPix, tblVoting
    WHERE tblVoting.picID = tblPix.picID
    AND numOfVotes >= 10
    ORDER BY picRating DESC, numOfVotes DESC LIMIT 10";
    Code (markup):
    That wouldn't quite do what I was thinking with the weighting idea where something with 1000 votes of 5 scores higher than 10 votes of 6. Maybe that's not a great idea anyway ;)
     
    void, Aug 22, 2006 IP
  11. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    For the weighted voting I was more thinking in the line of Void.

    But I couldn't find a weighted variable, but I found something that triggers my mind.
    http://www.codeproject.com/useritems/CodeProject_s_Voting_seq.asp

    Have to say it's been a long time I have done this kind of math.

    Anyways, here's a quote
    Now in my case I don't have registrered members so I can't use their status as a weight.

    But what about using total visits from an user based on their ip??
     
    123GoToAndPlay, Aug 23, 2006 IP