How to order by multiple fields with in MYSQL?

Discussion in 'PHP' started by phantom, Aug 17, 2009.

  1. #1
    I need help sorting with php mysql

    I have a list of items with ratings (people can rate from 1-5 stars)

    Now I want to show newly added items first and sort by rating and total votes

    so how do I show by highest rating but its only eligible if it has at least a total vote count of 20 votes but also sort by id (id DESC shows newest items)


    my fields are id, rate, total votes

    Any help would be greatly appreciated...thanks in advance!
     
    phantom, Aug 17, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Typically something like

    ORDER BY rate, id DESC;

    what that'll do is order by rate then by id , basically when the rating is the same it'll sort by ID

    anything more complex may need two queries.
     
    kblessinggr, Aug 17, 2009 IP
  3. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Ok thanks....any idea how can put some sort of if statement to see if total votes is higher than a certain numbers?
     
    phantom, Aug 17, 2009 IP
  4. Phynder

    Phynder Well-Known Member

    Messages:
    2,603
    Likes Received:
    145
    Best Answers:
    0
    Trophy Points:
    178
    #4
    Select * from table_name where total_vote_count>19 order by rate, id DESC;
     
    Phynder, Aug 17, 2009 IP
  5. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #5
    thanks but that still leaves all other items un viewable....any other ideas?
     
    phantom, Aug 17, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    So you want to see all items even with a total_vote_count less than 19? Then just do your processing in PHP

    if ($row['total_vote_count'] > 19) { .. }
     
    premiumscripts, Aug 17, 2009 IP