【问题标题】:Php - Laravel :Sum of rows in group by datePhp - Laravel:按日期分组的行总和
【发布时间】:2020-11-25 12:40:54
【问题描述】:

我有数据:

我想通过特定的 created_at 获得每行总和的结果。例如,“2020-10-12”日期的人工、租金等总和。

我尝试了查询,它给了我特定周的数据:

$cashFlowDetails =  CashModel::whereBetween('created_at', [
            now()->locale('en')->startOfWeek()->subWeek() ,
            now()->locale('en')->endOfWeek()->subWeek() ]) 
             ->get();

【问题讨论】:

  • 我应该通过查询还是使用 foreach 循环获得结果?
  • 你可以试试CashModel::whereDate('created_at', '2020-10-12')->sum('labour');
  • 如果我想得到所有行的总和怎么办?比如劳动力、租金、电费等
  • 您能否上传您的表的 sql 转储,其中包含我可以下载的某处的少量数据 - 然后我可以尝试为您想要的输出编写查询。因此,您可以对每个劳动力、租金、电费等进行单独查询
  • @DaminiSuthar 我猜你已经问过这个问题了

标签: php mysql laravel


【解决方案1】:

您可以在 CashModel 上使用 mutator 并附加一个新列,该列将分别保存每一行。

控制器:

public function index()
{
    return CashModel::whereBetween('created_at', [
        now()->locale('en')->startOfWeek()->subWeek(),
        now()->locale('en')->endOfWeek()->subWeek()
    ])->get();
}

型号:

public $appends = ['total_sum'];

public function getTotalSumAttribute($value)
{
    return $this->opening_balance +
        $this->labour +
        $this->rent +
        $this->electricity +
        $this->creditors +
        $this->gst +
        $this->insurance +
        $this->direct_debits +
        $this->other +
        $this->total_amount;
}

输出将是:

[
    {
        "id": 1,
        "labour": 215,
        "rent": 5412,
        "electricity": 1652,
        "creditors": 845,
        "gst": 161,
        "insurance": 332,
        "direct_debits": 1552,
        "other": 2165,
        "total_amount": 464,
        "created_at": "2020-11-15T00:00:00.000000Z",
        "updated_at": "2020-11-25T00:00:00.000000Z",
        "total_sum": 12798
    },
    {
        "id": 6,
        "labour": 771,
        "rent": 2087,
        "electricity": 1517,
        "creditors": 760,
        "gst": 2947,
        "insurance": 103,
        "direct_debits": 4915,
        "other": 4720,
        "total_amount": 2348,
        "created_at": "2020-11-21T04:16:48.000000Z",
        "updated_at": "2020-11-25T04:16:48.000000Z",
        "total_sum": 20168
    }
]

【讨论】:

    【解决方案2】:

    据我了解,您需要每个劳动力、电力等的日期总计。

    下面的查询应该会给你想要的结果。

    $cashFlowDetails = Cash::groupBy('created_at')
        ->selectRaw(
            'created_at,
             sum(labour) as labour,
             sum(rent) as rent,
             sum(electricity) as electricity,
             sum(creditors) as creditors,
             sum(gst) as gst,
             sum(insurance) as insurance,
             sum(direct_debits) as direct_debits,
             sum(others) as others, 
             sum(total_amount) as total_amount',        
        )
        ->whereBetween('created_at', [
            now()->locale('en')->startOfWeek()->subWeek() ,
            now()->locale('en')->endOfWeek()->subWeek() ])
        ->get();
    

    【讨论】:

      【解决方案3】:

      SUM(larbour) 作为 sum_labour,SUM(rent) 作为 sum_rent,等等。

      DB::table('table_name')
        ->select(DB::raw('SUM(labour) as sum_labour, DATE(created_at) as created_date'))
        ->groupBy(DB::raw('DATE(created_at)'))
        ->get();
      

      【讨论】:

        猜你喜欢
        • 2021-03-08
        • 1970-01-01
        • 2019-08-09
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-09
        相关资源
        最近更新 更多