Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用MySql按日期对数据求和_Php_Mysql_Sql - Fatal编程技术网

Php 使用MySql按日期对数据求和

Php 使用MySql按日期对数据求和,php,mysql,sql,Php,Mysql,Sql,我正试图为我的公司做一份财务数据报告: 我的表是\uuuuuu billabeldatas,如下所示: |--------|------------|----------|----------|--------------| | BIL_Id | BIL_Date | BIL_Type | BIL_Rate | BIL_Quantity | |--------|------------|----------|----------|--------------| | 1 | 2017

我正试图为我的公司做一份财务数据报告:

我的表是
\uuuuuu billabeldatas
,如下所示:

|--------|------------|----------|----------|--------------|
| BIL_Id | BIL_Date   | BIL_Type | BIL_Rate | BIL_Quantity |
|--------|------------|----------|----------|--------------|
| 1      | 2017-01-01 | Night    | 95       | 1            |
| 2      | 2017-01-02 | Night    | 95       | 1            |
| 3      | 2017-01-15 | Night    | 105      | 1            |
| 4      | 2017-01-15 | Item     | 8        | 2            |
| 5      | 2017-02-14 | Night    | 95       | 1            |
| 6      | 2017-02-15 | Night    | 95       | 1            |
| 7      | 2017-02-16 | Night    | 95       | 1            |
| 8      | 2017-03-20 | Night    | 89       | 1            |
| 9      | 2017-03-21 | Night    | 89       | 1            |
| 10     | 2017-03-21 | Item     | 8        | 3            |
|--------|------------|----------|----------|--------------|

我想要的是:

  • 第1个月(1月)=
    311.00$
    (95+95+105+8+8)

  • 第二个月(2月)=
    295.00$
    (95+95+95)

  • 第03个月(3月)=
    202.00$
    (89+89+8+8+8)

  • 第四个月(4月)=
    0.00$

  • 第五个月(五月)=
    0.00$


有没有可能用mySQL做到这一点

我需要问几个问题还是一个问题就可以问


感谢您的帮助。

首先使用子查询修改数据,使查询更易于以后操作(如果需要)


没有测试它,但这应该是正确的查询。它将返回当月的月份和金额。

您需要使用
Month()
函数和
分组方式

select month(BIL_Date) as month,
sum(BIL_Rate * BIL_Quantity) as sumval
from `___BillableDatas`
group by year(BIL_Date), month(BIL_Date);

我认为你也应该按年分组,除非你每年都有一张桌子

SELECT YEAR(BilDate) AS BillYear, MONTH(BilDate) AS BillMonth, SUM(BilRate * BilQuantity) AS Total
FROM __BillableDatas
GROUP BY YEAR(BilDate), MONTH(BilDate)

你可以用这个来达到类似的效果

select  month(BIL_DATE),
        sum(BIL_RATE * BIL_QUANTITY)
from    ___BillableDatas
group by year(BIL_DATE), month(BIL_DATE)
但在未来几个月内,它不会返回
0
s。 为了获得这些数据,您应该有一个包含所有月份的查找表,并在该表和该查询结果之间执行
左连接,如

select  t1.month, coalesce(t2.bill, 0)
from    months t1
left join (
            select  year(BIL_DATE) year,
                    month(BIL_DATE) month,
                    sum(BIL_RATE * BIL_QUANTITY) bill
            from    ___BillableDatas
            group by year(BIL_DATE), month(BIL_DATE)
        ) t2
on      t1.month = t2.month and
        t1.year = t2.year
编辑

如果您希望在应用程序级别操作数据,并且已有一个月数组可用,那么您可以在map
['month'->'bill']
中获取第一次查询的结果,然后在月数组中循环,如

for (month in months){
    if(map[month] == null){
        map.add(month, 0);
    }
}

请注意,这只是伪代码,因为我不精通PHP。

也不要忘记按年份分组;-)@拉胡尔。哇,谢谢。如果没有任何数据,我如何生成其他月份?类似于
从tablename组中按年份(BIL_日期)、月份(BIL_日期)选择BIL_日期、总和(BIL_比率*BIL_数量)看起来也不错。你知道如何为我还没有任何数据的witch生成其他月份的行吗?@qatari With MySQL,你需要一个数字表,没有数据的其他月份的详细信息可以来自另一个源,比如PHP中的数组?是的,如果你能在应用程序级别操作结果集,就不需要进行左连接。你可以得到你拥有的月份的值,然后用PHP将它们与月份列表相结合。请用你的想法更新你的第二个示例好吗?我用一些伪代码编辑了我的答案,只是想告诉你,如果我没有任何数据,我可以在其他月份生成什么?我可以使用数组吗?请问怎么做?感谢您的帮助。一种方法是使用预定义的数组<代码>$aStats=array()$aStats[1]=0$aStats[2]=0;etc
之后,在while中循环数据库结果:
$aStats[$aFetch['Month']]=$aFetch['Total']如果你变卖你的
$aStats
,你将有一整年的统计数据。但这是另一个问题。答案可以在这里找到:
select  t1.month, coalesce(t2.bill, 0)
from    months t1
left join (
            select  year(BIL_DATE) year,
                    month(BIL_DATE) month,
                    sum(BIL_RATE * BIL_QUANTITY) bill
            from    ___BillableDatas
            group by year(BIL_DATE), month(BIL_DATE)
        ) t2
on      t1.month = t2.month and
        t1.year = t2.year
for (month in months){
    if(map[month] == null){
        map.add(month, 0);
    }
}