selecting only a certain number of bits of data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • roondog

    selecting only a certain number of bits of data

    I have just started learning php and mysql. I am trying to make a news page
    for a site where you can enter news in a form and it appears on the news
    page. That is done, fairly simple. I've also ordered them with the latest
    first. I would now like to only show a certain number of the news articles.
    Say the latest five. Each article is numbered so I guess I would use that
    but i'm not really sure how to do it. Any ideas?



  • Tyno Gendo

    #2
    Re: selecting only a certain number of bits of data

    roondog wrote:
    I have just started learning php and mysql. I am trying to make a news page
    for a site where you can enter news in a form and it appears on the news
    page. That is done, fairly simple. I've also ordered them with the latest
    first. I would now like to only show a certain number of the news articles.
    Say the latest five. Each article is numbered so I guess I would use that
    but i'm not really sure how to do it. Any ideas?
    >
    >
    >
    If you have a date field you can order by date descending, but ID is
    fine if you happy with that... so...

    SELECT * FROM news ORDER BY news_id DESC LIMIT 5

    Comment

    • Toby A Inkster

      #3
      Re: selecting only a certain number of bits of data

      roondog wrote:
      I would now like to only show a certain number of the news articles.
      Say the latest five.
      Certain databases allow you to use a "LIMIT" clause to limit the number of
      results returned by a SELECT query. Because this is not part of the
      official SQL standard, the exact syntax varies between databases. Here are
      some examples:

      MySQL:
      SELECT * FROM articles ORDER BY created_date DESC LIMIT 5;

      PostgreSQL:
      SELECT * FROM articles ORDER BY created_date DESC LIMIT 5;

      Microsoft SQL Server:
      SELECT TOP 5 * FROM articles ORDER BY created_date DESC;

      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact
      Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

      * = I'm getting there!

      Comment

      • roondog

        #4
        Re: selecting only a certain number of bits of data


        "Toby A Inkster" <usenet200703@t obyinkster.co.u kwrote in message
        news:i7vbf4-4kp.ln1@ophelia .g5n.co.uk...
        roondog wrote:
        >
        I would now like to only show a certain number of the news articles.
        Say the latest five.
        >
        Certain databases allow you to use a "LIMIT" clause to limit the number of
        results returned by a SELECT query. Because this is not part of the
        official SQL standard, the exact syntax varies between databases. Here are
        some examples:
        >
        MySQL:
        SELECT * FROM articles ORDER BY created_date DESC LIMIT 5;
        >
        PostgreSQL:
        SELECT * FROM articles ORDER BY created_date DESC LIMIT 5;
        >
        Microsoft SQL Server:
        SELECT TOP 5 * FROM articles ORDER BY created_date DESC;
        >
        Thanks for that I knew there was something simple I was missing.


        Comment

        Working...