php order by mysql query

Discussion in 'PHP' started by Silver89, Jul 28, 2008.

  1. #1
    Hi,

    I'm trying to create a mysql query that will order by one type and then order the results after by another.

    The purpose is to get the sticked forum topics and show these in order before then showing the rest of the unsticked forum topics in numerical order.

    I've tried the following with no luck:

    SELECT * FROM forumPosts ORDER BY sticky DESC ORDER BY id DESC
    
    SELECT * FROM forumPosts ORDER BY sticky DESC id DESC
    PHP:
    Thanks in advance
     
    Silver89, Jul 28, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    SELECT * FROM forumPosts ORDER BY sticky, id DESC
    
    Code (sql):
     
    nico_swd, Jul 29, 2008 IP
  3. jessewhite77

    jessewhite77 Peon

    Messages:
    2
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This question is semi related to the original post but can anyone tell me why an Order by Clause might sort incorrectly with 3 parameters?

    $query="SELECT * FROM misc ORDER BY eYear, eMonth, eDay DESC";

    i run that query and it refuses to sort the months in DESC fashion they display in ASC, however the Year is in DESC. all values in the database are numeric 01-99 the year of course being 0000 format
     
    jessewhite77, Aug 1, 2008 IP
  4. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #4
    Try, replace the two ASC with DESC if you need,

    
    $query="SELECT * FROM misc ORDER BY eYear ASC, eMonth ASC, eDay DESC";
    
    
    PHP:
     
    Silver89, Aug 1, 2008 IP
  5. jessewhite77

    jessewhite77 Peon

    Messages:
    2
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Awesome, that worked like a charm! I was smacking myself in the forehead thinking why I hadn't tried that yet. Thanks!
     
    jessewhite77, Aug 1, 2008 IP
    Silver89 likes this.