ORDER BY question

Discussion in 'PHP' started by x0x, Jan 22, 2010.

  1. #1
    SELECT * FROM $t[table] ORDER BY rank ASC


    I want it to return info in ascending order (starting from smallest 'rank'), but leave the ones with 0 'rank' value to the end. Would that be possible?

    0
    0
    1
    0
    3
    6


    should come in this order

    1
    3
    6
    0
    0
    0


    Can it be done? Right now, obviously, it takes the ones with 0 value first... I don't want to skip them, I just want to leave them to the end.
     
    x0x, Jan 22, 2010 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    As a quick solution, following should work..

    SELECT *, IF(rank = 0, 99999, rank) AS new_rank FROM $t[table] ORDER BY new_rank ASC

    I hope there can be a better solution for this..
     
    mastermunj, Jan 22, 2010 IP
  3. millieleafos

    millieleafos Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I didn't know you could do that in sql, thanx
     
    millieleafos, Jan 23, 2010 IP
  4. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    EDIT: Got it to work this time. Thank you very much.
     
    Last edited: Jan 23, 2010
    x0x, Jan 23, 2010 IP
  5. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    For a moment I was wondering what went wrong in query that it did not work :D

    Anyways, happy that it is working now, however, is 0 default value for rank column? You can in that case keep it as MAX value, e.g. 9999 as default and you won't have to handle it separately in query. Just another way to handle it.
     
    mastermunj, Jan 23, 2010 IP