MySQL use of GROUP BY Error 1055

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45505313/article/details/102729617

1. Problem Cause

Execute the following SQL statement in MySQL 8.0.17 version, report an error Error 1055

select Host,Select_priv,user,COUNT(*)
FROM user
GROUP BY Select_priv,Host;

Error message:

Error 1055 - Expression #1 of SELECT list is not in GROUP BY clause 
and contains nonaggregated column 'mysql.user.Host' which
is not functionally dependent on columns in GROUP BY
clause; this is incompatible with sql_mode=only_full_group_by

Information from the wrong perspective, mainly sql_mode = only_full_group_byrelated. This is the group by the new features of version 5.7 or higher Mysql, after the 5.7 version only_full_group_by mode is enabled by default

ONLY_FUll_GROUP_BY 模式

  • GROUP BY operation for polymerization,If the field does not appear in the SELECT in a GROUP BY, SQL that this is not legitimate, because the field is not in the GROUP BY statement. Fact, that check out field must be included in the set of fields after a GROUP BY, or the aggregate functions which appear in the field

2. Solution

  1. First connection terminal entering mysql, using the following command to view global sql_mode, see the following theme

    SELECT @@GLOBAL.sql_mode;
    

    Here Insert Picture Description

  2. Sql_mode modified by command, in which the main ONLY_FUll_GROUP_BYis removed and re-arranged

     set @@GLOBAL.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
    
  3. Revisit sql_mode settings, confirm the change successful

    SELECT @@GLOBAL.sql_mode;
    

    Here Insert Picture Description

  4. Restart mysql service under mac

    mysql.server restart
    

3. Summary

In fact, strictly speaking, MySQL reason why the default open ONLY_FUll_GROUP_BYmode is also to reduce errors. SELECT field because when looking for is not GROUP BYthe polymerization field,Check out the field of data is not determined, when the index changes or the optimizer to select different optimization strategies may return different results. After this example, the sql_mode modify the database, the query statement is executed normally results are as follows. As can be seen from the whole theme data table,user field in fact there are four different values, but use the GROUP BY query results in only the first two, which changes the field conditions may also be found in other value after mean, resulting in failure

select Host,Select_priv,user,COUNT(*)
FROM user
GROUP BY Select_priv,Host;

Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_45505313/article/details/102729617