【问题标题】:Laravel Eloquent sum of multiplied columnsLaravel Eloquent 相乘列的总和
【发布时间】:2015-08-01 10:41:33
【问题描述】:

我在 mysql 数据库中有一个名为 transactions 的表,我将使用 Laravel 的 Eloquent ORM (Laravel 5) 来获得相乘列 (jumlah) 和 (harga) 的总和,这就是我的表的样子:

+----+----------+-------------+--------+---------+------------+---------------------+---------------------+
| id | order_id | nama_barang | jumlah | harga   | keterangan | created_at          | updated_at          |
+----+----------+-------------+--------+---------+------------+---------------------+---------------------+
|  4 |        3 | PS 4        |      2 | 4500000 | Hitam      | 2015-05-20 08:55:26 | 2015-05-20 08:56:14 |
+----+----------+-------------+--------+---------+------------+---------------------+---------------------+

我想得到这样的结果:

select sum(jumlah * harga) from transactions;
+---------------------+
| sum(jumlah * harga) |
+---------------------+
|             9000000 |
+---------------------+

如何用 ORM Laravel 做到这一点,我试过这个,但它给了我0

$amount = Transaction::all()->sum('(jumlah * harga)');

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    试试这个

    $amount = Transaction::select(DB::raw('sum(jumlah * harga) as total'))->get();
    

    【讨论】:

    • 语法错误,意外';'我认为它需要一个右括号。
    【解决方案2】:

    你必须在这里使用DB::raw方法:

    Transaction::all()->select(DB::raw('sum(jumlah * harga) AS sum_of_jumlah_harga'))
    

    【讨论】:

    • 这也给了我错误:调用未定义的方法 Illuminate\Database\Eloquent\Collection::select()
    【解决方案3】:

    雄辩的风格如下:

    $amount = Transaction::all()->sum(function($t){ 
        return $t->jumlah * $t->harga; 
    });
    

    【讨论】:

    • 这比在查询中求和要慢。检查我的答案。
    【解决方案4】:
      $particulars =DB::table('particulars as A')
            ->leftjoin('reqs as B', function ($join) {
                $join->on('A.particular_id', '=', 'B.particular_id');
            })
            ->whereBetween('B.date_issued', [$start_date, $end_date])
            ->select('A.item_name','A.unit','A.price','B.quantity_issued',DB::raw('sum(A.price*B.quantity_issued) as total_cost'))
            ->groupBy('A.particular_id')
            ->get();
    

    通过放置你的表名来使用类似的东西

    【讨论】:

      【解决方案5】:
      $amount = Transaction::sum(\DB::raw('jumla * harga'));
      

      这是更雄辩的完成。简而言之,它很快。

      另一个例子:

      $order_total = $order->products()->sum(\DB::raw('order_product.price * order_product.quantity'));
      

      【讨论】:

        【解决方案6】:

        你可以使用 Laravel 访问器

        https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

        public function getTotalAttribute()
        {
            return $this->all()->sum(function($transaction){
                return $transaction->jumlah * $transaction->harga; 
            });
        }
        At the top of your Order model put
        protected $appends = ['total'];
        you can get total like this (new Transaction)->total;
        

        【讨论】:

        • 这将检索表中每条记录的每一列,然后逐个循环遍历它们,将您所追求的位相加。这不是办法。
        • @Healyhatman 我再次阅读了这个问题,这是我认为这个问题的解决方案。但是是的,我们可以使用 where 和 first chain 方法根据您的需要自定义查询。
        • doncadavona frin 2018 下面的解决方案正是 OP 所追求的 :)
        【解决方案7】:

        答案分两步

        首先,得到总和

        $transaction = Transaction::select(\DB::raw('SUM(jumlah) as jumlak, SUM(harga) as harga'))
        ->first()
        

        然后做乘法

        $amount = $transaction->jumlah * $transaction->harga;
        

        【讨论】:

          【解决方案8】:

          我会说结果会完全不同。

          (a * b) + (c * d) 不等于 (a + c) * (b + d)

          【讨论】:

            【解决方案9】:

            您可以在 Laravel 8 中使用 SelectRaw 而无需调用 DB

             $amount = Transaction::selectRaw('sum(jumlah * harga) as total')->get();
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-07-28
              • 1970-01-01
              • 2020-02-28
              • 1970-01-01
              相关资源
              最近更新 更多