SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I was wondering how I can fetch the most recently added article from my Article table (and then the second and third most recently). I've saved a Date field for each article when it's added, so I'm guessing I need to do something with that. I could probably also do some complicated thing like figuring out the current date, subtracting every article date from the current date and then fetch the article that returned the smallest number.

    However, I'm thinking since fetching the "newest" or "most recent" post of a table must be pretty common, there probably is an easier SQL syntax for this.

    Is there?

    Thanks in advance !
    Sincerely,

    Eirik Johansen
    Netmaking AS

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try

    Select * from tablename ORDER by datefield DESC
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi fred,

    Great idea. And with some modifying, I can make it do exactly what I want. Like this:

    SELECT * FROM Article ORDER BY Date DESC LIMIT 1;

    Thanks for the suggestion. As always, it worked like a charm!
    Sincerely,

    Eirik Johansen
    Netmaking AS

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To add to freddydoesphp's advice, you can make your sql quesry more efficient by retrieving a result set of only the number of records you desire; ie, the three most recent, two most recent or the most recent. Substitute X below for the integer representing how many records you want returned.

    Select * from tablename ORDER by datefield DESC LIMIT X


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •