我有一周的当前总数,从星期一到星期一和上周总计。我一直在手动更新时间戳以反映当前和上周。我希望它只是将当前一周的数据从星期一到星期一以及从星期一到星期一的最后一周拉
本周代码。
echo "This Week <br />";
$result = mysql_query("SELECT * FROM form_2 WHERE timestamp BETWEEN '2011-11-7' AND '2011-11-14' AND checkcollec = 'yes'");
$num_rows = mysql_num_rows($result);
echo "<b>Checks Collected:</b> $num_rows ";
上周代码。
echo "<hr />Last Week <br />";
$result = mysql_query("SELECT * FROM form_2 WHERE timestamp BETWEEN '2011-10-31' AND '2011-11-07' AND checkcollec = 'yes'");
$num_rows = mysql_num_rows($result);
echo "<b>Checks Collected:</b> $num_rows ";
我怎么能这样做,所以我不必每周都手动更换它。
答案 0 :(得分:5)
将SQL-Part更改为
SELECT *
FROM form_2
WHERE TIMESTAMP BETWEEN DATE(NOW()) AND DATE_ADD(DATE(NOW()), INTERVAL -7 DAY)
AND checkcollec = 'yes';
如果NOW()
不是您需要的日期变量,只需将其替换为您需要的变量,例如:在某处的php脚本中设置它。
答案 1 :(得分:3)
$today = date("Y-m-d");
$todayMinusSeven = date("Y-m-d",strtotime("-7 days"));
$query = "SELECT * FROM form2 WHERE timestamp BETWEEN '$todayMinusSeven' AND
'$today' AND checkcollec = 'yes'";
echo $query;
答案 2 :(得分:2)
所有其他解决方案确实给出了过去7天的结果,因为我知道您希望从星期一到星期一以及前一周的那一周。 根据您拥有的SQL Server,您可以使用像
这样的week()函数WHERE week(timestamp) = week(NOW())
语法可能不同,也许您需要为周设置第二个参数,以便从星期一开始。
答案 3 :(得分:0)
本周:
SELECT * FROM form_2 WHERE timestamp BETWEEN curdate() - weekday(curdate()) AND curdate() - weekday(curdate()) + 7 AND checkcollec = 'yes'
上周:
SELECT * FROM form_2 WHERE timestamp BETWEEN curdate() - weekday(curdate()) - 7 AND curdate() - weekday(curdate()) AND checkcollec = 'yes'
答案 4 :(得分:0)
我认为问题是关于本周的数据显示。 TS称数据显示从星期一到星期一和最后几周从星期一到星期一。在我的建议中,我们可以使用WEEKOFYEAR函数在一周内获取数据。
本周:
SELECT *
FROM form_2
WHERE WEEKOFYEAR(timestamp)=WEEKOFYEAR(CURDATE())
AND checkcollec = 'yes'
上周:
SELECT *
FROM form_2
WHERE WEEKOFYEAR(timestamp)=WEEKOFYEAR(CURDATE())-1
AND checkcollec = 'yes'