代码之家  ›  专栏  ›  技术社区  ›  Alpha_Bords

预燃器月报

  •  0
  • Alpha_Bords  · 技术社区  · 6 年前

    我正在开发一个项目,这是一个酒店预订系统。

    我的问题是每个月都要循环,比如1是1月2日是2月等等。如果它是1,那么它将通过将它与 cout_created 在mysql数据库表的列中添加每个记录的所有利润,然后将其放入这样的数组中:

    $monthlyreport = [
       1 => 6000,
       2 => 5000,
       3 => 3000,
       4 => 12000,
       5 => 8000,
       6 => 4000,
       7 => 6000,
       8 => 9000,
       9 => 4000,
       10 => 6000,
       11 => 9000,
       12 => 4000,
    ]; 
    

    这是一个示例行的mysql数据库表架构:

     Row Name      Row Data
    
    id            9
    gst_id        1
    cout_tin      2018-07-01
    cout_tout     2018-07-02
    cout_nodays   1
    cout_single   1
    cout_double   2
    cout_family   1
    cout_roombal  13000
    cout_billbal  1120
    cout_totalbal 14120
    cout_downbal  6500
    cout_result   7620
    cout_created  2018-07-15 09:34:12
    

    我将PHP与CodeIgniter框架结合使用。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Dani Aguado    6 年前

    您需要做的是将数据库查询与PHP中的简单for循环结合起来。

    首先,您必须了解如何获取月份内的所有值。 cout_created 是时间戳/日期时间字段。 在MySQL中,可以使用如下查询获取数据:

    SELECT * FROM yourtable WHERE MONTH(cout_created) = XX

    使用此查询,在PHP中,您将执行以下操作:

    // Initialize the array
    $monthlyreport = array();
    // For each month we are gonna do the same
    for ($month = 1; $month <= 12; $month++) {
        // We get the results with database library, changing the sql according to our needs.
        $sql = "SELECT cout_result FROM yourtable WHERE MONTH(cout_created) = " . $month; 
        $query = $this->db->query($sql);
        // The accum variable to sum all the profits.
        $sum = 0;      
        // And foreach record, we sum the value to the actual one.
        foreach($query->result() as $row) {
           $sum = $sum + $row->cout_result;
        }
        // When finish, save the result on the array and start again.
        $montlyreport[$month] = $sum;
    }
    

    这是理解如何做的最简单的方法,但我们可以做得更好。MySQL允许我们使用其内置的 SUM() function 直接在mysql上,所以我们不需要在php上做额外的处理。我们可以做到这一点:

    // Initialize the array
    $monthlyreport = array();
    // For each month we are gonna do the same
    for ($month = 1; $month <= 12; $month++) {
        // But now we will get sum of the values instead of each value
        $sql = "SELECT SUM(cout_result) as profit FROM yourtable WHERE MONTH(cout_created) = " . $month; 
        $query = $this->db->query($sql);
        // And just save the profit
        $montlyreport[$month] = $query->row()->profit;
    }
    

    我没有测试它,因为这里没有用于测试的PHP环境,但是请告诉我它是如何工作的,我将相应地更新答案。


    编辑:我提供了另一个解决方案,该解决方案只对数据库进行一次查询,但这取决于数据库大小和记录数的性能:

    SELECT SUM(cout_result) as profit, MONTH(cout_created) as mymonth FROM yourtable GROUP BY MONTH(cout_created)

    有了这个,您只需要迭代 foreach 直接节省每一笔利润 mymonth

    $sql = "SELECT SUM(cout_result) as profit, MONTH(cout_created) as mymonth FROM yourtable GROUP BY MONTH(cout_created)"
    $query = $this->db->query($sql);
    $monthlyreport = array();
    foreach ($query->result() as $row) {
        $monthlyreport[$row->mymonth] = $row->profit;
    }