简体   繁体   中英

Select data uploaded in last 7 days

I want to select data from my table that was uploaded in the last 7 days. Here is what i have so far but it is not working.

$sql9="SELECT SUM(TruckDamage) 
WHERE DATEDIFF(`upload_date`, CURRENT_DATE) < 7 
AS     TotalTruckDamageSum FROM jwtdriversbank2";  
$result9=mysql_query($sql9);
$rows9=mysql_fetch_assoc($result9);
$sum8=$rows9['TotalTruckDamageSum'];
?>
<div>
Total Truck Repair Cost's: &pound;<?echo $sum8?><br>

Can anybody help please?

should be

sql9="SELECT SUM(TruckDamage) 

AS     TotalTruckDamageSum FROM jwtdriversbank2 WHERE DATEDIFF(`upload_date`, CURRENT_DATE()) < 7";  

The problem is that your query is invalid. You are assigning TotalTruckDamageSum to WHERE . Second syntax error is that FROM should be before WHERE . And last but not least is that bigger date should be first if you want to get non-negative result of DATEDIFF .

Should be:

$sql9="SELECT SUM(TruckDamage) AS TotalTruckDamageSum FROM `jwtdriversbank2` WHERE DATEDIFF(CURRENT_DATE,`upload_date`) <= 7";
$sql9="SELECT SUM(TruckDamage) AS     TotalTruckDamageSum 
                  where upload_date >= DATE_SUB(now(), INTERVAL 7 DAY)
                  FROM jwtdriversbank2";  

$result9=mysql_query($sql9);
$rows9=mysql_fetch_assoc($result9);
$sum8=$rows9['TotalTruckDamageSum'];
?>
<div>
Total Truck Repair Cost's: &pound;<?echo $sum8?><br>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM