Jump to content


Photo

Query from the last 7 days


  • Please log in to reply
6 replies to this topic

#1 Leovenous

Leovenous
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 18 July 2006 - 09:09 PM

I would think this is both a common and easy question, but 2 hours of digging have not yielded any results yet, so here goes.

All I want, is a simple query to get data from a table that has been modified in the last 7 days.

I have a field called 'modified' that has a time stamp 0000-00-00 00-00-00. It seems to me I want a way to check the timestamps and grab which ones are less than 7 days old.

So the query might look something like:

SELECT * FROM players WHERE modified  > (SUBDATE(CURDATE(), INTERVAL '7' DAY)

But obviously I'm bumping around in the dark.

Any help?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 18 July 2006 - 11:15 PM

one way

<?php
$date_7 = date('Y-m-d', strtotime('-7 days'));

$sql = "SELECT .... WHERE modified >= '$date_7' ";
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 18 July 2006 - 11:20 PM

SELECT * FROM players WHERE modified >= NOW() - INTERVAL 7 DAY

Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 18 July 2006 - 11:30 PM

Shouldn't that be NOW() or CURDATE()
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 18 July 2006 - 11:57 PM

Yup, sorry, my bad.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#6 Leovenous

Leovenous
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 19 July 2006 - 12:06 AM

Yep. That worked, and it makes sense. I wonder why I have not been able to find examples of queries even similar. (shrugs)

Thanks a bunch guys. Here's the final query:

$query = "SELECT * FROM players WHERE Creator_ID LIKE '$id' AND modified >= CURDATE( ) - INTERVAL 7 DAY";


#7 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 19 July 2006 - 01:07 AM

Personally, I'd use NOW(), not CURDATE(), in case you ever change your modified field to be of type DATETIME, not DATE.  FYI, make sure to always use the field name as the lvalue, so that an index can be used, if available.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users