Order by

Discussion in 'PHP' started by Alice24, Apr 5, 2011.

  1. #1
    i have a small issue
    i have this code $sql = "SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $start, $limit"; where my results are ordered by id, now what i want is to input a second order by "date" in the same querry. is it possible ?
     
    Alice24, Apr 5, 2011 IP
  2. phppro9x

    phppro9x Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When you want to sort or 'order by' multi columns, you just list the coloumn names in the order of their precedence (left > right), each separated by a comma.
    Your write again complete:
    $sql = "SELECT * FROM $tbl_name ORDER BY id DESC, date DESC LIMIT $start, $limit";
     
    phppro9x, Apr 5, 2011 IP
  3. Alice24

    Alice24 Greenhorn

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Thank you phppro9x i think this is the correct syntax, but my date is in this format 2011-04-05 and didn't sort by date too... should i change the date format ?
     
    Alice24, Apr 6, 2011 IP
  4. SametAras

    SametAras Well-Known Member

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #4
    Yes. Try to this SQL Query:

    ORDER BY date DATE_FORMAT(date , '%d/%m/%Y');
    
    Code (markup):
     
    SametAras, Apr 6, 2011 IP