If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > selecting data based on a date range function

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-20-04, 06:11
eggwater eggwater is offline
Registered User
 
Join Date: Jul 2004
Posts: 2
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?
Reply With Quote
  #2 (permalink)  
Old 07-21-04, 07:41
sbotu sbotu is offline
Registered User
 
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
Reply With Quote
  #3 (permalink)  
Old 07-21-04, 07:45
eggwater eggwater is offline
Registered User
 
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.
Reply With Quote
  #4 (permalink)  
Old 07-21-04, 14:28
Steve T. Steve T. is offline
Registered User
 
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
Reply With Quote
  #5 (permalink)  
Old 07-23-04, 00:43
sbotu sbotu is offline
Registered User
 
Join Date: Jul 2004
Posts: 3
Re To_Days

Glad it helped !
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On