PHP and MySQL: Order by most recent date and limit 10

前端 未结 6 1101
余生分开走
余生分开走 2020-12-11 15:13

I am building a notes system on my site and I\'ve got to the stage where users can post notes into the MySQL database using PHP and then PHP prints them out on a page. Howev

相关标签:
6条回答
  • 2020-12-11 15:50

    Do this

    $result = mysql_query("SELECT * FROM notes WHERE note_author_id='$u_id' ORDER BY date_time DESC LIMIT 0, 10");
    
    0 讨论(0)
  • 2020-12-11 15:51

    give like

     ORDER BY date_time DESC
    

    otherwise you are sorting them in ascending order.. thats why older ones come first

    0 讨论(0)
  • 2020-12-11 15:54

    This should do it :

    $result = mysql_query("SELECT * FROM notes WHERE note_author_id='$u_id' ORDER BY date_time DESC LIMIT 0, 10");
    
    0 讨论(0)
  • 2020-12-11 15:59

    use:

    SELECT * FROM notes WHERE note_author_id='$u_id' ORDER BY date_time DESC LIMIT 10
    

    DESC : descending order ( from newest to oldest ) LIMIT 10: first 10 records found.

    0 讨论(0)
  • 2020-12-11 16:03

    Try

    $result = mysql_query("SELECT * FROM notes WHERE note_author_id='$u_id' ORDER BY date_time DESC LIMIT 10");
    

    For a more detailed explanation on ORDER and LIMIT, visit the MySQL doc's articles about sorting rows and the basic select syntax (look for a bullet describing LIMIT).

    0 讨论(0)
  • 2020-12-11 16:06

    If in case you want your LIMIT to be a variable, here I named it $limit:

    "SELECT * FROM tbl ORDER BY input_date DESC LIMIT 0, $limit";
    
    0 讨论(0)
提交回复
热议问题