选择日期为sql和Sum

时间:2018-06-02 07:08:34

标签: php mysql sql pdo

美好的一天。我正在为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();
?>
&#13;
&#13;
&#13;

是否有查询可以选择一周中某一天的总和?或者我应该保留这段代码?

2 个答案:

答案 0 :(得分:4)

尝试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)

结果:

| pay_day | amount |
|---------|--------|
|       1 |    246 |
|       2 |    200 |
|      28 |   1702 |
|      29 |   1462 |
|      30 |    864 |
|      31 |   1092 |

测试数据:

| amount |             pay_date |
|--------|----------------------|
|    100 | 2018-06-02T00:00:00Z |
|    123 | 2018-06-01T00:00:00Z |
|    546 | 2018-05-31T00:00:00Z |
|    432 | 2018-05-30T00:00:00Z |
|    731 | 2018-05-29T00:00:00Z |
|    851 | 2018-05-28T00:00:00Z |
|    100 | 2018-06-02T00:00:00Z |
|    123 | 2018-06-01T00:00:00Z |
|    546 | 2018-05-31T00:00:00Z |
|    432 | 2018-05-30T00:00:00Z |
|    731 | 2018-05-29T00:00:00Z |
|    851 | 2018-05-28T00:00:00Z |
|    436 | 2018-05-27T00:00:00Z |

SQL Fiddle Demo Link

如果您想了解更多信息,那么

您可以使用weekday()DAYNAME()

SELECT WEEKDAY(pay_date) WEEKDAY,DAYNAME(pay_date) DAYNAME,SUM(amount) amount 
FROM tbl_paid WHERE pay_date >= NOW() - INTERVAL 7 DAY 
GROUP BY WEEKDAY(pay_date),DAYNAME(pay_date)

结果:

| WEEKDAY |   DAYNAME | amount |
|---------|-----------|--------|
|       0 |    Monday |   1702 |
|       1 |   Tuesday |   1462 |
|       2 | Wednesday |    864 |
|       3 |  Thursday |   1092 |
|       4 |    Friday |    246 |
|       5 |  Saturday |    200 |
|       6 |    Sunday |   1287 |

SQL Fiddle Demo Link

答案 1 :(得分:1)

如果您必须通过日期,请执行:

SELECT DATE(pay_date) `DATE`, SUM(amount) daily_amount
FROM tbl_paid 
WHERE DATE(pay_date) BETWEEN DATE(:today) - INTERVAL 1 WEEK AND DATE(:today)
GROUP BY DATE(pay_date);

否则使用数据库的当前日期。

SELECT DATE(pay_date) `DATE`, SUM(amount) daily_amount
FROM tbl_paid 
WHERE DATE(pay_date)>=CURRENT_DATE - INTERVAL 1 WEEK
GROUP BY DATE(pay_date);

您不需要七次运行查询。记得;不要重复自己(干)。