将mysql查询转换为查询生成器

时间:2019-04-02 02:24:24

标签: laravel query-builder

您好,我是laravel查询构建器的新手。 我想将此查询转换为laravel查询生成器。预先谢谢你。

$bo_facilities = ' SELECT 
a.bo_facility_code,
a.bo_facility_groupcode,
a.bo_number,
a.bo_indYesOrNo,
a.bo_indLogic,
b.bo_content_facility_description

FROM 

bo_facilities AS a 

RIGHT JOIN bo_content_facilities AS b
ON b.bo_content_facility_code = a.bo_facility_code AND b.bo_content_facility_facilityGroupCode = a.bo_facility_groupcode
WHERE a.bo_hotel_code =  "1" GROUP BY b.bo_content_facility_description'; 

我的groupBy方法无效。.

$join->groupBy('b.bo_content_facility_description');

这是我的整个代码 我不知道最好的方法。

DB::table('bo_facilities AS a')
->select("a.bo_facility_code", "a.bo_facility_groupcode", "a.bo_number", "a.bo_indYesOrNo", "a.bo_indLogic", "b.bo_content_facility_description")
->join('bo_content_facilities AS b', function ($join) { 
    $join->on('b.bo_content_facility_code', '=', 'a.bo_facility_code'); 
    $join->on('b.bo_content_facility_facilityGroupCode', '=', 'a.bo_facility_groupcode');
    $join->groupBy('b.bo_content_facility_description');
    })
->where('a.bo_hotel_code', $hotelCode)
->get();

1 个答案:

答案 0 :(得分:0)

最后我已经解决了我的问题。

$bo_facilities_raw = DB::table('bo_facilities AS a')
->join('bo_content_facilities AS b', function ($join) {
    $join->on([['b.bo_content_facility_code', '=', 'a.bo_facility_code'], ['b.bo_content_facility_facilityGroupCode', '=', 'a.bo_facility_groupcode']]); 
    })
->select("a.bo_hotel_code","a.bo_facility_code", "a.bo_facility_groupcode", "a.bo_number", "a.bo_indYesOrNo", "a.bo_indLogic", "b.bo_content_facility_description", "b.bo_content_facility_code", "b.bo_content_facility_facilityGroupCode")
->where('a.bo_hotel_code', 1)
->get();

$bo_facilities_decode = json_decode($bo_facilities_raw, true);

$remove_dupli = array_unique(array_column($bo_facilities_decode, 'bo_content_facility_description'));

$bo_facilities = array_intersect_key($bo_facilities_decode, $remove_dupli);

记录的重复出现在多维数组内部,这就是为什么我使用数组唯一和数组相交键删除重复描述的原因。