Click to See Complete Forum and Search --> : Help with mysql data select
roondog
04-14-2007, 01:05 PM
I'm working through some php tutorials and am making a news script. In my table I have the articles numbered they also have a headline, a timestamp and the story. What I want to do is only select stories from a certain month and then put them in order starting with the latest. I know i can use ORDER BY but how would only select those from a certain month.
Znupi
04-14-2007, 01:14 PM
I think (I'm not sure) this should work:
SELECT * FROM `tablename` WHERE `timestamp` > 'DATE' AND `timestamp` < 'DATE' + INTERVAL 1 MONTH
Where DATE is the first day of the month you wish to view results for. For example: '2007-04-14' .
NogDog
04-14-2007, 08:31 PM
I might do:
$year_month = '200704';
$sql = "SELECT * FROM `table` WHERE DATE_FORMAT(`timestamp`, '%Y%m') = '$year_month'";
$result = mysql_query($sql) or die("Query failed ($sql): " . mysql_error());