在 Laravel 中使用不同的多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44382353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:05:21  来源:igfitidea点击:

use distinct with multiple columns in laravel

laravellaravel-5.3laravel-eloquentlaravel-query-builder

提问by HirenMangukiya

my table structure is as below

我的表结构如下

date        seller  unit    price   total
05-06-17    abc     14      700     9800
05-06-17    pqr     12      600     7200
05-06-17    abc     10      520     5200
06-06-17    abc     10      600     6000
06-06-17    pqr     15      520     7800
06-06-17    pqr     16      520     8320

I need to fetch record like

我需要获取记录

1) 'date' = '05-06-2017', 'seller' = 'abc', 'total' = '15000'
2) 'date' = '05-06-2017', 'seller' = 'pqr', 'total' = '7200'
3) 'date' = '06-06-2017', 'seller' = 'abc', 'total' = '6000'
4) 'date' = '06-06-2017', 'seller' = 'pqr', 'total' = '16120'

I need data from database in date wise. sellerabchas two entry in a table for date05-06-2017so I need totalof that day for sellerabcas well pqrsame things for the second datefor all seller

我需要按日期从数据库中获取数据。sellerabc对表中的两个入口date05-06-2017,所以我需要total的那一天了sellerabc,以及pqr同样的事情第二个date所有seller

回答by Manish Yadav

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

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

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

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

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

Explanation

解释

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

DB::select('orders')->select('date', 'seller', DB::raw('SUM(total) as total'))orders在我们的数据库中查询我们的表,然后获取我们提供的列,即date, seller and sum of total column as total

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

->groupBy('seller')->groupBy('date')它将合并同一卖家在同一日期的多条记录。

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

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

回答by linktoahref

You could try

你可以试试

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

回答by AhmedSeaf

$search = $request->search;

    $productSearch = Product::where('title', 'LIKE', '%'.$search.'%')
                    ->orWhere('description', 'LIKE', '%'.$search.'%')
                    ->get();

       return $produtcSearch;

回答by Saurav

You should use advance search query. Here, this might help you

您应该使用高级搜索查询。在这里,这可能对你有帮助