选择过去7天内上传的数据

时间:2013-08-12 09:02:20

标签: php mysql database date

我想从我的表中选择过去7天内上传的数据。这是我到目前为止所做的,但它不起作用。

$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>

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

应该是

sql9="SELECT SUM(TruckDamage) 

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

答案 1 :(得分:0)

问题是您的查询无效。您正在将TotalTruckDamageSum分配给WHERE。第二个语法错误是FROM应该在WHERE之前。最后但并非最不重要的是,如果您希望得到DATEDIFF的非负结果,则应该首先使用更大的日期。

应该是:

$sql9="SELECT SUM(TruckDamage) AS TotalTruckDamageSum FROM `jwtdriversbank2` WHERE DATEDIFF(CURRENT_DATE,`upload_date`) <= 7";

答案 2 :(得分:0)

$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>