Click to See Complete Forum and Search --> : MySQL query to select all entries but from most recent month of most recent year


Space Goat
02-02-2006, 09:09 AM
I'm trying to develop a MySQL query to select all entries of a table but those of the most recent month of the most recent year. This is for an archive page containing links from all previous months but this one; this month's links reside on the front page.

Here's what I have now:

$query = "SELECT MAX(year(date)) AS maxyear FROM fplinks";
$result = mysql_query ($query);
$array = mysql_fetch_array ($result);
$maxyear = $array['maxyear'];

$query1 = "SELECT MAX(MONTH(date)) AS maxmonth FROM fplinks WHERE YEAR(date)=$maxyear";
$result1 = mysql_query ($query1);
$array1 = mysql_fetch_array ($result1);
$maxmonth = $array1['maxmonth'];

$query2 = "SELECT DATE_FORMAT(date, '%M %e, %Y') AS link_date, DAYOFYEAR(date) AS link_day, YEAR(date) AS link_year FROM fplinks WHERE MONTH(date)!=$maxmonth AND YEAR(date)!=$maxyear GROUP BY link_date ORDER BY link_year DESC, link_day DESC";
But, for some reason, the archive page is excluding links from January of this year, even though I've posted links for February.

Can anyone tell me how to improve the query so it only excludes entries from the most recent month of the most recent year (i.e., this month)?

NogDog
02-02-2006, 01:47 PM
Assume `date` is the date field in table `table`:

SELECT * FROM `table`
WHERE DATE_FORMAT(`date`, '%Y%m') != DATE_FORMAT(CURDATE(), '%Y%m')

Space Goat
02-02-2006, 05:15 PM
Thanks, that appears to have worked. :)