解决MySQL:1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated

1.SQL

SELECT
    B.MODULE_CODE AS ID,
    B.PARENT_MODULE_CODE AS PID,
    CASE
    WHEN 
        ( A.MODULE_NAME IS NOT NULL ) 
    THEN
        A.MODULE_NAME 
    ELSE 
        B.MODULE_NAME 
    END AS NAME,
    A.URL AS URL,
    A.URL_TYPE AS URL_TYPE,
    A.MODULE_TARGET AS TARGET,
    B.ORDERBY AS ORDERBY 
FROM
    SYS_ROLE_TREE B
LEFT JOIN 
    SYS_MODULE A 
ON 
    A.MODULE_CODE = B.MODULE_CODE
LEFT JOIN 
    SYS_ROLE_USER_RELATION R 
ON 
    B.ROLE_CODE = R.ROLE_CODE
LEFT JOIN 
    SYS_ROLE L 
ON 
    B.ROLE_CODE = L.ROLE_CODE 
WHERE
    L.STATUS = '1' 
GROUP BY
    ID
ORDER BY
    ORDERBY

2.错误

> 1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'gas_web_gb.B.parent_module_code' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
> 时间: 0.008s

3.原因

是由于默认的 MySQL 配置中 sql_mode 配置了 only_full_group_by,需要 GROUP BY 中包含所有 在 SELECT 中出现的字段。

4.only_full_group_by:使用这个就是使用和oracle一样的group 规则, select的列都要在group中,或者本身是聚合列(SUM,AVG,MAX,MIN) 才行。

5.使用 SQL 语句可以查询这个配置

SELECT @@sql_mode;

6.解决

如果是Linux,就在配置文件(my.cnf)中修改 sql_mode 的配置(在/usr/local/etc/my.cnf路径下)。如果是Windows,就修改配置文件my.ini,我的是Windows,如下所示:

7.重启服务就好了

posted @ 2019-08-30 20:41  Java-Legend  阅读(14449)  评论(0编辑  收藏  举报