Welcome to the MacNN Forums.

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.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > help with SQL query of date range

help with SQL query of date range
Thread Tools
Fresh-Faced Recruit
Join Date: Jul 2005
Status: Offline
Reply With Quote
Jul 9, 2005, 08:14 PM
 
table AAA
id integer
last_checked_date date

I would like to do a query that displays all of the ID's of records whose "last_checked_date" is equal to or more than 30 days in the past. In other words, if it's been 30 days or more since I last did something with that ID, then give me the ID of that record.

I've tried something like this, but it didn't work. There's probably a simpler solution that will work correctly.
SELECT * FROM AAA a
WHERE ( ADDDATE( a. last_checked_date, INTERVAL 30 DAY ) >= curdate() )


What should the query look like? I've tried a few things, but I haven't gotten it to work yet. I'm using MySQL 3.x, and my web host may be using a higher version, so I'd like to find a standard approach that will work across versions.

Thanks!
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Jul 9, 2005, 08:29 PM
 
Originally Posted by saturdayis850
table AAA
id integer
last_checked_date date

I would like to do a query that displays all of the ID's of records whose "last_checked_date" is equal to or more than 30 days in the past. In other words, if it's been 30 days or more since I last did something with that ID, then give me the ID of that record.

I've tried something like this, but it didn't work. There's probably a simpler solution that will work correctly.
SELECT * FROM AAA a
WHERE ( ADDDATE( a. last_checked_date, INTERVAL 30 DAY ) >= curdate() )


What should the query look like? I've tried a few things, but I haven't gotten it to work yet. I'm using MySQL 3.x, and my web host may be using a higher version, so I'd like to find a standard approach that will work across versions.

Thanks!
It may be simpler to create the SQL exactly as you described it:-

SELECT * FROM AAA a
WHERE a.last_checked_date <= DATE_SUB(NOW(), INTERVAL 30 DAY)
Computer thez nohhh...
     
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Jul 9, 2005, 10:42 PM
 
If you'll just be using MySQL, you could also use the MySQL TO_DAYS function. Something like this should work:

Code:
SELECT * from Table WHERE ((TO_DAYS(last_checked_date) - TO_DAYS(NOW()) <= 30);
The only thing necessary for evil to flourish is for good men to do nothing
- Edmund Burke
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:20 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2