很好的一天。我选择的日期mysql和我选择它一个接一个。我想选择所有的一个星期。我以前是一个人选的。
这是我的密码:
<?php
$chart6 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 6 DAY");
$chart6->execute(array(":today"=>$today));
$chartData6 = $chart6->fetchColumn();
$chart5 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 5 DAY");
$chart5->execute(array(":today"=>$today));
$chartData5 = $chart5->fetchColumn();
$chart4 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 4 DAY");
$chart4->execute(array(":today"=>$today));
$chartData4 = $chart4->fetchColumn();
$chart3 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 3 DAY");
$chart3->execute(array(":today"=>$today));
$chartData3 = $chart3->fetchColumn();
$chart2 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 2 DAY");
$chart2->execute(array(":today"=>$today));
$chartData2 = $chart2->fetchColumn();
$chart1 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today - INTERVAL 1 DAY");
$chart1->execute(array(":today"=>$today));
$chartData1 = $chart1->fetchColumn();
$chart0 = $controller->runQuery("SELECT SUM(amount) FROM tbl_paid WHERE pay_date >= :today");
$chart0->execute(array(":today"=>$today));
$chartData0 = $chart0->fetchColumn();
?>
有没有查询可以选择一周中全天的总和?或者我应该保留这个密码?
2条答案
按热度按时间ie3xauqp1#
如果您必须通过日期,请执行以下操作:
否则使用数据库的当前日期。
您不需要运行查询七次。记得;不要重复(干的)。
f2uvfpb92#
试试sql使用
group
```SELECT Day(pay_date) pay_day,SUM(amount) amount
FROM tbl_paid WHERE pay_date >= NOW() - INTERVAL 6 DAY
GROUP BY Day(pay_date)
结果:
sql fiddle演示链接