MySQL group by causes ERROR 1055 problem

MySQL group by causes ERROR 1055 problem

wrong description

ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘work_ad.api_community_pic.id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

problem causes:
ONLY_FULL_GROUP_BY的意思是:对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中,也就是说查出来的列必须在group by后面出现否则就会报错,或者这个字段出现在聚合函数里面。
Solution:

Query database configuration:

select @@GLOBAL.sql_mode;
select @@SESSION.sql_mode;

STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Modify the database configuration

set @@sql_mode 
='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

Note: If the database already exists, you need to use the database before modifying

sql_mode configuration

ONLY_FULL_GROUP_BY

For GROUP BY aggregation operations, if the column in the SELECT does not appear in the GROUP BY, then this SQL is illegal because the column is not in the GROUP BY clause. In short, the columns following the SELECT must be included by the columns following the GROUP BY. Such as:
select a,b from table group by a,b,c; (correct)
select a,b,c from table group by a,b; (error)
This configuration will make the GROUP BY statement environment very narrow, so Generally do not add this configuration

NO_AUTO_VALUE_ON_ZERO

This value affects the insertion of self-growing columns. By default, inserting 0 or NULL means that the next self-increasing value is generated. (If you don’t believe me, you can try, the default sql_mode you set in the auto-increment primary key column is 0, the field will automatically become the latest auto-increment, the effect is the same as null), if the user wants to insert a value of 0 (not changed), The column is self-increasing, then this option is useful.

STRICT_TRANS_TABLES

In this mode, if a value cannot be inserted into a transaction table, the current operation is interrupted, and there is no restriction on the non-transaction table. (InnoDB default transaction table, MyISAM default non-transaction table; MySQL transaction table supports the unified submission or rollback of batch processing as a complete task, that is, multiple statements contained in the transaction are executed or not executed. Transaction tables do not support this kind of operation. If a statement in the batch encounters an error, the statement before the error is executed successfully, and the following statement is not executed; MySQL transaction table has table locks and row locks, but only table locks are available for non-transactional tables)

NO_ZERO_IN_DATE

In strict mode, zero days and months are not allowed

NO_ZERO_DATE

Set this value, mysql database does not allow the insertion of a zero date, and inserting a zero date will throw an error instead of a warning.

ERROR_FOR_DIVISION_BY_ZERO

During INSERT or UPDATE, if the data is divided by zero, an error is generated instead of a warning. If this mode is not given, MySQL returns NULL when the data is divided by zero

NO_AUTO_CREATE_USER

Prohibit GRANT from creating users with empty passwords

NO_ENGINE_SUBSTITUTION

If the required storage engine is disabled or not compiled, then an error is thrown. When this value is not set, replace with the default storage engine and throw an exception

PIPES_AS_CONCAT

Treat "||" as a string concatenation operator instead of an or operator, which is the same as the Oracle database and similar to the string concatenation function Concat

ANSI_QUOTES

When ANSI_QUOTES is enabled, double quotes cannot be used to quote a string because it is interpreted as an identifier

Reference link

Guess you like

Origin blog.csdn.net/weixin_45609519/article/details/105827563