duang8642 2013-04-30 11:06 采纳率: 0%
浏览 186

在Laravel Fluent中使用Distinct

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():

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 field which 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').

Can anyone point me to how can I remove my duplicate records? I bet this is another forehead slapper for me.

  • 写回答

1条回答 默认 最新

  • dsa88886666 2013-08-12 11:32
    关注

    In my project I tried distinct() and groupby() too and both of them worked:

    //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():

    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:

    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'));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么