Laravel(GROUP BY)中的查询生成器

时间:2019-03-23 05:40:59

标签: php mysql laravel-query-builder

$bo_facilities = DB::select('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 =  "'.$hotel['code'].'"  GROUP BY b.bo_content_facility_description');

错误消息:

"message": "SQLSTATE[42000]: Syntax error or access violation: 1055
'philex.main.a.bo_facility_code' isn't in GROUP BY (SQL: 

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 = "'.$hotel['code'].'"
GROUP  BY b.bo_content_facility_description 

)",

由于GROUP BY方法,我遇到了此错误。

1 个答案:

答案 0 :(得分:0)

看看mysql中的aggregate functions

如果按b.bo_content_facility_description对结果进行分组,则SELECT中不在GROUP BY中的其他字段应与SUM,{{1 }}等

按该单个字段分组而不对其他字段进行任何调整是有意义的,因为您没有指定如何组合其他字段以获取该分组字段的单个记录。

我建议您对预期的结果保持头脑清醒。