Results 1 to 4 of 4

Thread: Sort rows, does if / else work in select?

  1. #1
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sort rows, does if / else work in select?

    Hello I got a problem when Im going to sort one of the tables I have.
    If one of the the tables column is current_number > 0 I want to sort it by the current number but if the column is current_number < 1 I want to sort it by the start_number but when I select the tables I cant get the if or else to work.. it looks like this now:

    HTML Code:
       $listing_sql = "select ..... some stuff and.....  IF(s.status, s.specials_status, p.current_number) as final_number.
    The IF statement is still in the Select section so Is it possible to place an else or something like that? final_number is the one Im later on use sort desc / asc with.

    hmm to clear this up a bit, lets say I have those four numbers in the two columns:
    10 - start_number (this one have current_number 0)
    15 - current_number
    20 - start_number (this one have current_number 0)
    25 - current_numbers

    now I want to sort them with a button called "Sort number", The button is using the name final_number that I made when I selected the tables (see the html code). Is it even possible to mix them and then sort them.

    Basically I wanna know if its possible to use two different columns as the same Alias but during certain circumstances.

    Hope you understand what im talking about
    Last edited by Dennis_Gull; 04-27-2007 at 04:21 PM.

  2. #2
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I fixed it by adding a new column but Im still interested if theres another solution.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    SELECT x FROM y ORDER BY IF(current_number < 1, start_number, current_number);
    ... surely.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •