Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Posts
    2

    Unanswered: selecting data based on a date range function

    hi

    I'm trying to fugure out a query that will select records from within a range of two dates using MySQL.

    The data I'm trying to represent is for a news page on a website. so I want to have 'current stories' i.e. those from the past 30 days. I'd then like to list the stories that are 30-60 days old and anything else would be in an archive.

    I have this query for the 0-30 day old stories:

    SELECT headline, dateposted, newsID, flag
    FROM news
    WHERE newstype = 9 AND flag = 0 And DATE_ADD(CURDATE(), INTERVAL -30 DAY) <=dateposted
    ORDER BY dateposted

    this works well, my problem is I can't find any document for choosing data that's between x and y days old.

    can anyone help?

  2. #2
    Join Date
    Jul 2004
    Posts
    3

    Re dates between a set number of days

    Have you tried the To_days function?
    It goes something like this


    SELECT dateposted, to_days(now())
    - TO_DAYS(dateposted)
    AS NumDays
    FROM news

    NumDays will give you how old the article is going back from today's date

    so to get a range you would use

    SELECT dateposted, to_days(now()) - TO_DAYS(dateposted) AS NumDays
    FROM news
    where to_days(now()) - TO_DAYS(dateposted) between 30 and 60

    hope this helps
    Karen

  3. #3
    Join Date
    Jul 2004
    Posts
    2
    Yes this is the method I used to solve it...

    thanks anyway.

    I should've said I'd solved my problem on my own... bit too busy sorry.

  4. #4
    Join Date
    Sep 2003
    Location
    So. Cal. USA
    Posts
    142
    Ah, but now I've learned how to do it, too. Thank you both!
    --ST

  5. #5
    Join Date
    Jul 2004
    Posts
    3

    Re To_Days

    Glad it helped !

Posting Permissions

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