【问题标题】:How to get price and sum the price as a total price according to date?如何根据日期获取价格并将价格汇总为总价?
【发布时间】:2017-10-09 07:32:04
【问题描述】:

这是我的密码

 $filter = DB::table('detail_clothes')
            ->get(['id', 'clothes_detail_date', 'clothes_price'])
            ->groupBy(function($date){
                return Carbon::parse($date->clothes_date)->format('m/Y');
            });

$result = [];
$clothes_total_price = 0;
$clothes_date = null;

foreach ($filter as $dn => $dn_value) {
    $clothes = Clothes::where('clothes_date', $dn)
               ->first();

    foreach ($dn_value as $k => $dnval) {
        $clothes_total_price += $dnval->clothes_price;
        $result[$dn]['clothes_total_price'] = $clothes_total_price;
    }

    $date_temp = date_format(date_create_from_format('Y-m-d', $request->clothes_detail_date),'m/Y');
}

我有两个模型:衣服和细节衣服

Clothes : id, clothes_date, clothes_total_price
DetailClothes : id, clothes_detail_date, clothes_price

示例:: 当我输入衬衫价格时,它会进入详细的衣服并将其存储在那里,它也会存储在衣服中作为衣服总价格

它会根据月份显示所有记录,但是当我总结它时,它不会根据我想要的显示,

我想要的例如: 首先,如果我本月输入两次1000的价格,总价应该是2000,如果我输入两次下个月的价格1000,总价应该是2000而不是4000

其次,如果我输入日期示例: 2017-08-25 ,它将存储到两个模型,但特别是对于 CLOTHES 模型,它总是会根据月份和年份将日期更新为最新提交的日期,

example:
at Detail Clothes model it should be like this::
1st submit : clothes_detail_date : 2017-08-25, clothes_price : 1000
2nd submit : clothes_detail_date : 2017-08-01, clothes_price : 2000, 

expected result:
at Clothes model it should be like this::       
clothes_date : 2017-08-25, clothes_total_price: 3000

注意* 在 Clothes Model 中,它只会根据月份和年份显示 1 行记录,并且永远不会在同一月份和年份显示 2 条记录

谁能帮帮我??????

【问题讨论】:

  • 您希望如何通过循环在 PHP 中获得结果或直接从查询中获得结果?
  • 另外,您能否给出示例表和数据的 SQL,以便我们可以尝试使用 SQLFiddle?
  • @DhavalPurohit 我是新手,所以我真的不明白它是如何工作的,
  • @wast 等我更新问题
  • @DhavalPurohit 查询的直接结果怎么样

标签: php mysql laravel


【解决方案1】:

我认为您忘记将 $clothes_total_price 重置为零。我也将 1 行从内部 foreach 移到了外部。

foreach ($filter as $dn => $dn_value) {
   $clothes_total_price = 0;
   $clothes = Clothes::where('clothes_date', $dn)
           ->first();

   foreach ($dn_value as $k => $dnval) {
     $clothes_total_price += $dnval->clothes_price;
   }
   $result[$dn]['clothes_total_price'] = $clothes_total_price;
   $date_temp = date_format(date_create_from_format('Y-m-d', 
     $request->clothes_detail_date),'m/Y');
}

编辑:SQLFiddle 中的附加答案

您按一个月分组,取该月的最大日期,然后对当月的价格求和:

INSERT INTO 
  Clothes (clothes_date, clothes_total_price)
  SELECT * FROM (
    SELECT 
       MAX(clothes_detail_date) new_clothes_date
      ,SUM(clothes_price) new_clothes_total_price
    FROM DetailClothes
    GROUP BY DATE_FORMAT(clothes_detail_date,'%Y%m') 
  ) newTable
ON DUPLICATE KEY UPDATE    
clothes_date=newTable.new_clothes_date
,clothes_total_price=newTable.new_clothes_total_price
;

【讨论】:

  • 感谢@wast 的回答,第二个问题是如果存在我上面提到的其他创建,如何更新,如何存储或更新?
  • 请更新问题并提供示例数据和您期望得到的数据。另外,如果正确,请标记为答案。
  • @AntonyLim 我已经为您添加了 INSERT ON DUPLICATE KEY UPDATE 查询。看看吧。
  • 感谢@wast,但更新不起作用,它仍然会在数据库中创建一个新行,虽然日期已经相同,它应该是一个日期一个记录,
  • @AntonyLim 您必须在该列上设置唯一键,它应该可以工作。
【解决方案2】:

我已经知道怎么回答这个问题了

DetailClothes 模型

public function scopeStoreDetailClothes($query, $request){
    $data = $request->all();
    DetailClothes::create($data)->save();

    $date = Carbon::parse($request->clothes_detail_date);

    $filter = DetailClothes::whereMonth('clothes_detail_date', '=', $date->month)
            ->whereYear('clothes_detail_date', '=', $date->year);

    $total = (object) [
            'clothes_price' => $filter->sum('clothes_price'),
    ]; 

    $detail_clothes = DetailClothes::whereMonth('clothes_detail_date', '=', $date->month)
            ->whereYear('clothes_detail_date', '=', $date->year)
            ->orderBy('clothes_detail_date', 'desc')
            ->first();

    $clothes = Clothes::whereMonth('clothes_date', '=', $date->month)
            ->whereYear('clothes_date', '=', $date->month)
            ->first();

    Clothes::updateOrCreate(
        [
            'clothes_date' => isset($clothes->clothes_date) ? $clothes->clothes_date : null
        ], [
            'clothes_date' => isset($detail_clothes) ? $detail_clothes->clothes_detail_date : $request->clothes_detail_date,
            'clothes_total_price' => $total->clothes_price
        ]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    相关资源
    最近更新 更多