Multiple ORDER BY sequence

Discussion in 'PHP' started by goneinsane, Jul 21, 2010.

  1. #1
    Here's the tail end of some code I have in place.
    DB_TABLE."users.ID ".$addon." ORDER BY ".DB_TABLE."tickets.`status` ASC;");
    PHP:
    I would like it to ORDER BY the status first and then by ID, however, the following is not working for me.
    DB_TABLE."users.ID ".$addon." ORDER BY ".DB_TABLE."tickets.`status` ASC;", "tickets.`ID` DESC;");
    PHP:
    I also tried:
    DB_TABLE."users.ID ".$addon." ORDER BY ".DB_TABLE."tickets.`status` ASC, tickets.`ID` DESC;");
    PHP:
    What is it that I am doing wrong here? Any help is appreciated. Thanks.
     
    goneinsane, Jul 21, 2010 IP
  2. ashishhbti

    ashishhbti Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can use multiple column in order by clause.

    Order by coloum1 ASC, colum2 ASC & so on..


    So I think your 2nd statement is correct. I think it will work perfectly.......
     
    ashishhbti, Jul 21, 2010 IP
  3. bencummins

    bencummins Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Any errors?
     
    bencummins, Jul 21, 2010 IP
  4. goneinsane

    goneinsane Well-Known Member

    Messages:
    303
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #4
    It didn't work when I tried it. The whole list just disappears then.
     
    goneinsane, Jul 21, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    Hi,

    ashishhbti has replied your question but here is once again
    
    ......... "ORDER BY ".DB_TABLE."tickets.`status` ASC, ".DB_TABLE."tickets.ID DESC;")
    
    PHP:
    Regards
    ps: seems you forgot to concatenate DB_TABLE for the second order rule
     
    koko5, Jul 22, 2010 IP
  6. upl8t

    upl8t Peon

    Messages:
    80
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    When you're building anything but the simplest sql queries in php it's a good habit during the development stage to echo your sql statements as you build them. This way you can see the query the way it would be run. You can copy the output from the echo and run it directly on your mysql server using your favourite sql query tool. If the query generates the results you're looking for you're good to go. When you start concatenating a combination of text and variables it's easy to miss a quote or comma. By echoing the sql it's much easier to see the actual sql. And don't forget to remove the echo before going live.
     
    upl8t, Jul 22, 2010 IP
  7. goneinsane

    goneinsane Well-Known Member

    Messages:
    303
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #7
    Thank you so much, that worked perfect.
     
    goneinsane, Jul 22, 2010 IP