Jump to content

MAX(`Date`) not to exceed 2 days into the future


travelkind

Recommended Posts

I was wondering if there was a way to have the MAX function NOT return a Date that is more than 2 days into the future (from the current day)?  If there is a Date that is more than 2 days into the future I would like to return the one closest to the current day.  Here is the code I have:

 

<?php

mysql_connect("local", "xxx", "xxx") or die(mysql_error());
mysql_select_db("pricelink") or die(mysql_error());



// Get a specific result from the "ft9_fuel_tax_price_lines" table
$query ="SELECT ItemNumber,TableCode,Cost, MAX(`Date`) as `max_date`, MAX(`Time`) as 'max_time' FROM `ft9_fuel_tax_price_lines`
         GROUP BY `ItemNumber`,`TableCode`";

$result = mysql_query($query) or die(mysql_error());

echo "<table border='1'>";
echo "<tr> <th>ItemNumber</th> <th>TableCode</th> <th>Date</th> <th>Time</th> <th>Cost</th> </tr>";


// keeps getting the next row until there are no more to get
while($row=mysql_fetch_array($result))
{

// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['ItemNumber'];
        echo "</td><td>";
        echo $row['TableCode'];
        echo "</td><td>";
        echo $row['max_date'];
        echo "</td><td>";
        echo $row['max_time'];
        echo "</td><td>";
        echo $row['Cost'];
echo "</td></tr>"; 


} 

echo "</table>";
?>

 

Any help would be appreciated.

Thanks!

How are you storing `Date` in your table?  What is the column type?

 

The column type in MYSQL is "DATE" format.

 

Just checking.  Barand's code will do the trick.

 

Edit: obviously replacing .... with your subsequent code as well as adding in your additional GROUP BY and such

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.