且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在laravel中使用不重复的多列

更新时间:2023-02-02 23:11:38

每当我们有多行具有相同数据的行需要在最终输出中合并在一起时,我们将使用 group by 功能> mySQL .

Whenever we have multiple rows with same data that needs to be merged together in final output, we use group by functionality of mySQL.

因此,在您的情况下,您可以使用laravel查询构建器以这种方式进行操作.

so in your case you can use laravel query builder to do it in this way.

 DB::table('orders')->select('date', 'seller', DB::raw('SUM(total) as total'))
    ->groupBy('seller')
    ->groupBy('date')
    ->get();

说明

DB :: select('orders')->选择('date','seller',DB :: raw('SUM(total)as total')')将查询我们数据库中的 orders 表&然后获取我们提供的列,即日期,卖方和总列的总和作为总金额

DB::select('orders')->select('date', 'seller', DB::raw('SUM(total) as total')) will query our orders table in our database & then fetch our provided columns i.e. date, seller and sum of total column as total

-> groupBy('seller')-> groupBy('date')它将在同一日期合并同一卖方的多个记录.

->groupBy('seller')->groupBy('date') It will merge the multiple records of same seller in same date.

-> get(); 我们正在从查询中获取数据.

->get(); we are get getting our data from the query.