How to SELECT the most recent entry and the one before it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • londres9b
    New Member
    • Apr 2010
    • 106

    #1

    How to SELECT the most recent entry and the one before it?

    I have a table called news.
    The table is some like that:

    - id -- title -- article -- date -
    - -
    - - - - - - - - - - - - - - - - -

    What I want to do is SELECT the most recent entry (not sure if Date field is necessary for that) and also the one before it.

    So if I have this:


    - id -- title -- article -- date -

    1 -- News1 -- Article1 -- 1/1/1

    2 -- News2 -- Article2 -- 2/1/1

    3 -- News3 -- Article3 -- 3/1/1

    4 -- News4 -- Article4 -- 4/1/1

    - - - - - - - - - - - - - - - - - -

    What I want is:
    • On day 1/1/1 the code gives the News 1
    • On day 2/1/1 the same code gives the News 2 and 1
    • On day 3/1/1 the " " " " News 3 and 2
    • On day 4/1/1 the " " " " News 4 and 3



    Is it possible? How?

    SELECT * from news WHERE ???

    Please help me thanks.

    =============== ===

    SOLVED

    I've found the answer, it is posted below.

    =============== ====
    Last edited by londres9b; Aug 1 '10, 09:59 PM. Reason: SOLVED
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Please post your solutions to help other people who search for the same problem.

    Comment

    • londres9b
      New Member
      • Apr 2010
      • 106

      #3
      The Solution

      Here's how to solve the above problem:

      Code:
      SELECT * from [B]news[/B] WHERE [B]date[/B] between date_sub(curdate(), interval 1 day) and curdate();
      Hope this helps somebody as it helped me.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        What I want to do is SELECT the most recent entry (not sure if Date field is necessary for that) and also the one before it.
        What if the most recent entry is not today? Your solution would not work if that is the case.

        If you want the latest entries from a database, you can simply use the ORDER BY clause on an integer primary key that uses AUTO_INCREMENT. (Or something equivalent.)
        For example:
        [code=sql]SELECT * FROM news
        ORDER BY id DESC
        LIMIT 2;[/code]
        It basically just flips the table over and returns the top (previously bottom) 2 entries.

        If you don't have a integer PK, then you would have to pick some other appropriate field. Such as a timestamp that stores the time when the row was created.

        Comment

        • londres9b
          New Member
          • Apr 2010
          • 106

          #5
          Originally posted by Atli
          What if the most recent entry is not today? Your solution would not work if that is the case.

          If you want the latest entries from a database, you can simply use the ORDER BY clause on an integer primary key that uses AUTO_INCREMENT. (Or something equivalent.)
          For example:
          [code=sql]SELECT * FROM news
          ORDER BY id DESC
          LIMIT 2;[/code]
          It basically just flips the table over and returns the top (previously bottom) 2 entries.

          If you don't have a integer PK, then you would have to pick some other appropriate field. Such as a timestamp that stores the time when the row was created.
          You're right thank you

          Comment

          Working...