如何查询Last Week From Thursday to Wednesday?
How do I query Last Week From Thursday to Wednesday?
我正在尝试查询从上周的星期四早上 12:00AM 到星期三晚上 12:00pm 的记录。
这需要始终运行并且是最新的。我对从星期四到星期三的本周有另一个查询,它确实有效。我只需要另一个查询来显示上周过去一周后的结果。
我认为此查询是针对上周的,但不是从周四开始到周三结束的。
SELECT collector, SUM('amountcollected') as 'totalsum' FROM commissioninfo
WHERE thedate >= Curdate() - INTERVAL Dayofweek(Curdate())+6 day
AND thedate < Curdate() - INTERVAL Dayofweek(Curdate())-1 day
GROUP BY collector ORDER BY totalsum DESC
使用 MySQL 自己的日期函数可能有更好的方法,但我想我可能会这样做。不过,我假设您指的是星期三晚上 11:59:59pm,而不是中午:
$lastThursday = new DateTime("Thursday last week");
$wednesday = new DateTime("Wednesday");
$today = new DateTime("now");
$day = $today->format("l");
if ($day == "Sunday" || $day == "Monday" || $day == "Tuesday" || $day == "Wednesday") {
$lastThursday->modify("-1 week");
$wednesday->modify("-1 week");
}
$query = "SELECT collector, SUM('amountcollected') as 'totalsum' FROM commissioninfo WHERE thedate BETWEEN ".$lastThursday->format("Y-m-d")." AND ".$wednesday->format("Y-m-d ")." GROUP BY collector ORDER BY totalsum DESC";
我正在尝试查询从上周的星期四早上 12:00AM 到星期三晚上 12:00pm 的记录。
这需要始终运行并且是最新的。我对从星期四到星期三的本周有另一个查询,它确实有效。我只需要另一个查询来显示上周过去一周后的结果。
我认为此查询是针对上周的,但不是从周四开始到周三结束的。
SELECT collector, SUM('amountcollected') as 'totalsum' FROM commissioninfo
WHERE thedate >= Curdate() - INTERVAL Dayofweek(Curdate())+6 day
AND thedate < Curdate() - INTERVAL Dayofweek(Curdate())-1 day
GROUP BY collector ORDER BY totalsum DESC
使用 MySQL 自己的日期函数可能有更好的方法,但我想我可能会这样做。不过,我假设您指的是星期三晚上 11:59:59pm,而不是中午:
$lastThursday = new DateTime("Thursday last week");
$wednesday = new DateTime("Wednesday");
$today = new DateTime("now");
$day = $today->format("l");
if ($day == "Sunday" || $day == "Monday" || $day == "Tuesday" || $day == "Wednesday") {
$lastThursday->modify("-1 week");
$wednesday->modify("-1 week");
}
$query = "SELECT collector, SUM('amountcollected') as 'totalsum' FROM commissioninfo WHERE thedate BETWEEN ".$lastThursday->format("Y-m-d")." AND ".$wednesday->format("Y-m-d ")." GROUP BY collector ORDER BY totalsum DESC";