php 在 Laravel Fluent 中使用 Distinct
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16298368/
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
Using Distinct in Laravel Fluent
提问by Glenn Williams
I have this join:
我有这个加入:
Return DB::table('volunteer')
->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
->select(array('*','volunteer.id AS link_id'))
->where('is_published', '=', 1)
But it unsurprisingly returns duplicate records, so I try to use distinct()
:
但不出所料,它返回重复的记录,所以我尝试使用distinct()
:
Return DB::table('volunteer')
->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
->select(array('*','volunteer.id AS link_id'))
->distinct()
->where('is_published', '=', 1)
but I want to use distinct()
on a specific single fieldwhich I'd easily be able to do in SQL. It seems distinct()
does not take parameters, i.e. I can't say distinct('volunteer.id')
.
但我想distinct()
在一个特定的单个字段上使用,我很容易在 SQL 中做到这一点。好像distinct()
不带参数,即我不能说distinct('volunteer.id')
。
Can anyone point me to how can I remove my duplicate records? I bet this is another forehead slapper for me.
谁能指出我如何删除重复记录?我敢打赌这对我来说是另一个额头拍击器。
回答by totymedli
In my project I tried distinct()
and groupby()
too and both of them worked:
在项目中,我试图distinct()
和groupby()
也和他们两个的工作:
//Distinct version.
Company_Customer_Product::where('Company_id', '=', $companyid)->distinct()->get(array('Customer_id'));
//Goup by version.
Company_Customer_Product::where('Company_id', '=', $companyid)->groupby('Customer_id')->get(array('Customer_id'));
According to this, distinct()
should work in your case too, just use it with get()
:
据此,也distinct()
应该适用于您的情况,只需将其用于get()
:
Return DB::table('volunteer')
->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
->select(array('*','volunteer.id AS link_id'))
->distinct()
->where('is_published', '=', 1)
->get(array('volunteer.id'));
Otherwise you don't need distinct()
when you use groupby()
so you could just use:
否则你distinct()
在使用时不需要,groupby()
所以你可以使用:
Return DB::table('volunteer')
->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
->select(array('*','volunteer.id AS link_id'))
->group_by('volunteer.id')
->where('is_published', '=', 1)
->get(array('volunteer.id'));