为了账号安全,请及时绑定邮箱和手机立即绑定

sql查询到laravel查询与联合

sql查询到laravel查询与联合

PHP
月关宝盒 2023-05-12 15:11:54
我想将下面的联合查询转换为 laravel 查询DB::select("select lead_master.lead_stage from lead_master inner join  user_master on user_master.user_name=lead_master.assigned_user_name where region in (".$geo_string.") and teams in (".$filter_teams_string.") group by (lead_stage) unionselect  meeting_hash from meetings_master where assigned_user_name in (".$filter_username_string.") and meeting_hash in ('follow_up','first_time') group by (meeting_hash)  union select sales_stage from opportunity_master where sales_stage in ('Identified','QO_to be  approved') and assigned_user in (".$filter_username_string.") group by (sales_stage)");以下查询在 postgre 中正常运行
查看完整描述

1 回答

?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

在开始之前,我假设你了解laravel Eloquent

假设您的lead_master 表是LeadMaster Model,也是MeetingsMasterandOpportunityMaster


$lead_master = LeadMaster::select("lead_master.lead_stage as stage")

    ->join('user_master','user_master.user_name','=','lead_master.assigned_user_name')

    ->whereIn('region',$geo) //$geo is an array

    ->whereIn('teams',$filter_username) //also an array

    ->groupBy('lead_stage');


$meetings_master = MeetingsMaster::select('meeting_hash as stage')

    ->whereIn('assigned_user_name',$filter_username)

    ->whereIn('meeting_hash',['follow_up','first_time'])

    ->groupBy('meeting_hash');


$opportunity_master = OpportunityMaster::select('sales_stage as stage')

    ->whereIn('sales_stage',['Identified','QO_to be approved'])

    ->whereIn('assigned_user',filter_username)

    ->groupBy('sales_stage');


$query = $lead_master->union($meetings_master)->union($opportunity_master)->pluck('stage'); //your result

dd($query);

这就是你使用Laravel Eloquent时的样子


ps:你不需要为此制作3个变量,我只是用它来方便维护


查看完整回答
反对 回复 2023-05-12
  • 1 回答
  • 0 关注
  • 107 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信