Need help with Mysql Order by statement

Discussion in 'PHP' started by phantom, Jul 21, 2009.

  1. #1
    I have a highscore list for a game that has 3 modes easy, normal or hard

    In the DB the row is :

    Score - Mode
    350 normal
    300 easy
    300 hard
    300 normal

    I want to list the games by highest score yet still have the modes grouped together.
    For instance, all Hard mode scores will be listed from highest to lowest then all normal then all easy.

    So when done would be
    Score - Mode
    300 hard
    350 normal
    300 normal
    300 easy

    Any Idea what the query would be to do this. I have tried order by score, mode but that doesn't work.

    Any help would be great!

    Thanks
     
    phantom, Jul 21, 2009 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Use 2 fields in your order by clause:

    ...ORDER BY Mode ASC, Score DESC...

    However, because you are sorting the Mode and it is a text field, it is going to put them in order alphabetically--so 'hard' will always appear between 'normal' and 'easy', no matter which way you sort it. To force it to be at the top or bottom you would need to make a new 'Mode' table and assign ranks to them (i.e hard has rank 1, normal rank 2, etc.) then you join the tables and order them by that rank.
     
    plog, Jul 21, 2009 IP
  3. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    what about a PHP script that does all that once it pulls all the database information.
    then it would just be less-than or greater-than comparisons to the Modes of hard, normal, easy.
    That would sort all Hard modes first by choosing all the hard modes first For Each, While -- etc.

    Have a PHP script pull all the Mode of Hard first.
    Then have the script sort them based on highest and lowest integer. and stack them nicely as you want.
    Do the same thing for Normal and Easy.

    That way it doesnt matter how you store them in the database.
     
    ezprint2008, Jul 21, 2009 IP
  4. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #4
    @plog ok I wil try it thanks
    @ezprint2008 wouldn't that be more than one query though?

    EDIT: Plog it worked like a charm! Thanks again!
     
    phantom, Jul 21, 2009 IP
  5. bncplix

    bncplix Banned

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can combine queries

    Here is an example one i may have


    Select USERNAME as user, PASSWORD as pass, EMAIL as useremail FROM members WHERE whatever

    Then u can access it all

    Or look up joint queries
     
    bncplix, Jul 21, 2009 IP