Group_concat - laravel 雄辩

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25847738/
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 10:06:13  来源:igfitidea点击:

Group_concat - laravel eloquent

laraveleloquentgroup-concat

提问by user3481058

please I want to use group_concat in a query using eloquent and not raw queries.

请我想在使用 eloquent 而不是原始查询的查询中使用 group_concat。

here is the code which i tried to execute and did't work for me:

这是我尝试执行但对我不起作用的代码:

commands::join('products', 'products.id', '=','commands.idproduct')
->select('commands.username','**group_concat(products.name)**')
->group by ('commands. username')
->get();

Thanks in advance :)

提前致谢 :)

回答by user3481058

I just used use DB; and in my query I used DB::raw('group_concat(products.name)') !!

我刚用过 use DB;在我的查询中,我使用了 DB::raw('group_concat(products.name)') !!

回答by Uttam Panara

Best example for it..

最好的例子..

 
ModelName::select('ID', DB::raw('CONCAT(First_Name, " ", Last_Name) AS full_name'))
           ->get()
           ->toArray();

Result 
   Jon Doe,Jeffery Way,Tailer,taylor otwell

回答by Sharath

This worked for me

这对我有用

$list = TableName::where('user_id', 'user_001'
        ->groupBy('user_id')
        ->groupBy('subscription_id')
        ->select('user_id','subscription_id','type')
        ->selectRaw('GROUP_CONCAT(holiday) as holidays')
        ->get();

or

或者

use Illuminate\Support\Facades\DB;

$sql = 'SELECT GROUP_CONCAT(holiday) as holidays, user_id,subscription_id, type FROM TableName 
        where vendor_id = 'user_001' GROUP BY user_id, subscription_id;';
$list = DB::select($sql, []);

回答by cabs

or just replace

或者只是更换

->select('commands.username','**group_concat(products.name)**')

with

->selectRaw('commands.username, **group_concat(products.name)**')

回答by Nisar P

This worked for me: (9.0+)

这对我有用:(9.0+)

DB::raw('string_agg(products.name, \',\') as products')

You will need to use Illuminate\Support\Facades\DB; for this.

您将需要使用 Illuminate\Support\Facades\DB;为了这。

回答by srinu rathod

$data=\DB::table('paging_config') ->leftjoin('paging_groups', 'paging_config.page_group', '=', 'paging_groups.page_number') ->leftjoin('spk_mnt', 'paging_groups.ext', '=', 'spk_mnt.stn_no') ->select('paging_config.page_group','paging_groups.ext', 'spk_mnt.stn_status') ->selectRaw('GROUP_CONCAT(DISTINCT description) as description') ->where('paging_config.page_group','=','paging_config.page_group') ->groupBy('paging_config.description')

$data=\DB::table('paging_config') ->leftjoin('paging_groups', 'paging_config.page_group', '=', 'paging_groups.page_number') ->leftjoin('spk_mnt', 'paging_groups.ext' , '=', 'spk_mnt.stn_no') ->select('paging_config.page_group','paging_groups.ext', 'spk_mnt.stn_status') ->selectRaw('GROUP_CONCAT(DISTINCT description) as description') ->where ('paging_config.page_group','=','paging_config.page_group') ->groupBy('paging_config.description')

        ->get();