SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Doing a Query based on Date - HELP needed!!

    I would like to get some help with a query that I can't seem to figure out.

    I have this table with a DATE-column. The query should only select those rows that covers the last 5 days and save it into e.g. $result

    I bet it's quite easy to do...so please someone help me a.s.a.p. :-)

    Thx in advance!

    /tahoo

  2. #2
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to

    1) to order descendingly your rows by date
    2) Select the first 5 rows,

    here's the query

    Code:
    SELECT DATACOLUM as result FROM tablename ORDER BY columndate DESC LIMIT 5;

  3. #3
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it sounds like he wants stuff from the last 5 days, not the 5 most recent things. if so, this should do that:

    Code:
    SELECT whatever FROM table WHERE date_col >= DATE_SUB(NOW(), INTERVAL 5 DAYS) ORDER BY date_col DESC
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  4. #4
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes...you're right Dr LarryPepper! :-)

    That was what I wanted to do - and it worked just fine! (when I removed the "S" from "DAYS" that you wrote :-))

    Thanks!


    One last thing on this issue...I would like to print the result in opposite direction. As it is now the results comes out with the post made 5 days ago on top. I would like to go the other way with the latest post on top.

    Do you have a suggestion? Or maybe someone else can help me?

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Deleting the word DESC in the query will sort the results the opposite way to how they are sorted now

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    SitePoint Member
    Join Date
    Oct 2001
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    Lost my mind there for a while...of course!

    Thanx!

  7. #7
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by seanf
    Deleting the word DESC in the query will sort the results the opposite way to how they are sorted now
    Sorry, yes just remove the keyword DESC from the query, thanks Sean


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
  •