如何通过联接两个表来获取数据时获取唯一的单列值

时间:2018-10-30 10:58:35

标签: laravel

$data['establishments2'] = Establishments::Join("establishment_categories",'establishment_categories.establishment_id','=','establishments.id')->where('establishments.city','LIKE',$location)->where('establishments.status',0)->whereIn('establishment_id',array($est_data))->get(array('establishments.*'));

这是控制器状态。

我有两个表,在表1中,我将ID与表2匹配,然后从表1中获取数据,在表2中,我在表1中具有多个具有相同ID的值。我只想一次获取表1值的数据,但是由于我在表2中有多个具有相同ID的数据,数据正在重复多次,有人可以告诉我如何仅一次获得具有单个ID或具有相同ID的多个值的惠特表2的数据...谢谢

1 个答案:

答案 0 :(得分:0)

您可以通过选择所需的字段名称来实现 而是从表场所获取所有字段

$data['establishments2'] = Establishments::Join("establishment_categories",'establishment_categories.establishment_id','=','establishments.id')->where('establishments.city','LIKE',$location)->where('establishments.status',0)->whereIn('establishment_id',array($est_data))->get(array('establishments.*'));

您可以从表机构中选择特定字段,例如

 $data['establishments2'] = Establishments::Join("establishment_categories",'establishment_categories.establishment_id','=','establishments.id')->where('establishments.city','LIKE',$location)->where('establishments.status',0)->whereIn('establishment_id',array($est_data))->get('establishments.fieldName');

或者您也可以

$data['establishments2'] = `Establishments::Join("establishment_categories",'establishment_categories.establishment_id','=','establishments.id')->where('establishments.city','LIKE',$location)->where('establishments.status',0)->whereIn('establishment_id',array($est_data))->select('establishments.fieldName')->get();`