PDA

View Full Version : Limiting results by date


kiwipenguin
09-16-2003, 04:55 AM
Hi there

I've written a small PHP module that pulls information out of a mySQL database to display sports results on a webpage. I've got just about everything I need out of it, but I'd like to limit the results to those with a date within the last 2 weeks.

Here's my SQL query

SELECT * FROM Sport WHERE Game='$array' ORDER BY Date DESC


There's a field named "Date" in the table, how do I limit the results from this?

gish
09-16-2003, 03:02 PM
Limit? you mean only get back the Date values?

SELECT Date FROM Sport WHERE Game='$array' ORDER BY Date DESC
--or--
SELECT * FROM Sport WHERE Game='$array' AND Date = '10/10/2001' ORDER BY Date DESC

if this is not what you mean I am sorry I missunderstood your question.

kiwipenguin
09-16-2003, 08:11 PM
Geez, sorry about that. That question was really poorly worded.

Each record in the database has a "date" field, and I want to return only results that have a date within the last two weeks.

Would the following work?


<?

$date = date("Y-m-d");

$result = mysql_query("SELECT * FROM Sport WHERE Game='$array' AND Date-'$date'>14 ORDER BY Date DESC",$db);

?>

gish
09-17-2003, 12:20 PM
I would make a current date var, as you did, then I would create another that is 2 weeks difference.....then query based on that

sorry my example will not be PHP, but the SQL wil be the same

var CurrentDate
var TempDate = CurrentDate - 14 days

SELECT * FROM Sport WHERE Game='$array' AND Date >= #$TempDate# ORDER BY Date DESC",$db

I am not sure how the date object will be handled with '-14' .