在 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
use distinct with multiple columns in laravel
提问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. seller
abc
has two entry in a table for date
05-06-2017
so I need total
of that day for seller
abc
as well pqr
same things for the second date
for all seller
我需要按日期从数据库中获取数据。seller
abc
对表中的两个入口date
05-06-2017
,所以我需要total
的那一天了seller
abc
,以及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 orders
table 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;