简体   繁体   English

在 laravel 查询中获取字段

[英]fetching field in laravel query

I have two tables user_master and user_teams with common field user_name.我有两个表 user_master 和 user_teams 具有公共字段 user_name。 I want to join the table and get the team value group by teams tried as我想加入表格并按团队尝试获取团队价值组

$filter_teams = DB::table('user_teams')
->join('user_master','user_master.user_name','=','user_teams.user_name')
->whereIn('user_master.geo',$geo)
->groupBy('user_teams.team')
->pluck('user_teams.team')
->toArray();

by may values are duplicating.I'm using postgresql可能值是重复的。我正在使用 postgresql

since you didn't determine select fields... the default will be '*' this is why you are getting duplicated fields...因为您没有确定 select 字段...默认为 '*' 这就是您获得重复字段的原因...

just add:只需添加:

->select('user_teams.team') 

and i think that all.我认为就是这样。

i recommend not using group without aggregation.... so my advice your query to like this:我建议不要使用没有聚合的组......所以我建议你的查询是这样的:

$filter_teams = DB::table('user_teams')
->join('user_master','user_master.user_name','=','user_teams.user_name')
->whereIn('user_master.geo',$geo)
->select('user_teams.team')->distinct()
->pluck('user_teams.team')
->toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM