MySQL error: in aggregated query without group by, incompatible with sql_mode = only_full_group_by

tags: IT  mysql  sql  database

Scenario: The same product, different models, there are different prices, such as 1 mobile phone has 128G, 2,256g, 2500 yuan, but the product display page, the price shows 2,000 yuan, if it is a product, no The method is displayed, the method used here is to query the average price of the same ID, compare the first price, if different, return 0, the same returns 1, through 0, 1, let the front end to determine.

SQL: Use the AVG function to query the average price, compare 0 or 1 with the first Price, but report error
 Because after mysql5.7.5, the default SQL configuration above Mysql 5.7.5 is: SQL_Mode = "Only_Full_Group_by", this configuration strictly implements "SQL92 Standard". Therefore, some SQL before the previous SQL could not be executed normally. In fact, it is caused by our SQL irregular, because some of the data returned after the groupbook, so this error will occur.

When SQL is executed, this cause, simply:
Due to the setting of the ONLY_FULL_GROUP_BY, if the SELECT field is not in Group By,
And the field of SELECT does not use the aggregate function (SUM, AVG, MIN, MIN, etc.), then this SQL query is illegal by mysql, will be reported.

ONLY_FULL_GROUP_BY This model features

1: As long as there is a function of aggregate functions, count (), max (), avg (), etc., you will need to use Group By, otherwise the above error will be reported.
2: GROUP BY ID (ID is the primary key), SELECT has no problem, including the polymer function
3: When the group by role (non-primary key), SELECT can only be a polymeric function and role (Group By field), otherwise error

There are two solutions:
First, modify the mysql configuration file:
Find my mysql installation directory, open the My.ini file directly with Notepad
Edit the My.cnf file, add content under the [MySQL] tab

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION


Restart the MySQL service

Second, modify the SQL statement:
Using a function any_value () contains an error field

SELECT ANY_VALUE(avg( price )= price) FROM pms_sku_stock ss WHERE ss.product_id =27

Any_Value () function description:

mysql has an ANY_VALUE (FIELD) function, its main role is to suppress the only_full_group_by value being rejected. Such a SQL statement is in the ONLY_FULL_GROUP_BY mode, and it can be performed normally in the open mode, and is not rejected by MySQL. Any_Value () will select the specified column value of the first data in the data being divided into the same group as the return data. Officially has an introduction, address: https://dev.mysql.com/doc/refman/5.7/EN/miscellaneous-functions.html#function_any-view

Reference link:MySQL Error --This IS INCOMPATIBLE with SQL_MODE = ONLY_FULL_GROUP_BY Perfect Solution

Intelligent Recommendation

MySQL Query: Order by Clause Is Not in Group by..this is incompatible with sql_mode = only_full_group_by

When using MySQL, the following query is performed: The error message is as follows: The wrong reason is that my mysql version is 5.7, using the following statement query you know It is set by default...

[Mysql] SQL statement group query encountered error: this is incompatible with sql_mode = only_full_group_by, compatibility solving

This problem occurs above Mysql5.7 because the default SQL configuration is: SQL_MODE = "Only_Full_Group_by". If it is developed directly on 5.7, it is recommended to use the Any_Value () fu...

Mysql Group by error: 1055-Expression #1GROUP BY this is incompatible with sql_mode=only_full_group_by

Original link: https://blog.csdn.net/loveliness_peri/article/details/88051316   The reason for the problem: there are more in sql_mode:ONLY_FULL_GROUP_BY Use SQL statements to query this configur...

Solution: MySQL Error 1055: Group by clause; this is incompatible with sql_mode = only_full_group_by

When using MySQL to perform Group By query statement, error: problem causes The SQL standard does not allow the Select list, a Having condition statement, or a grouse in the Group By in the Order by s...

More Recommendation

Laravel query mysql error Laravel this is incompatible with sql_mode = only_full_group_by

During the query with laravel, Daravel this is incompatible with sql_mode = only_full_group_by Looked online, because MySQL version is compatible, I use 5.7, add [mysql] and [mysqld], add additional f...

Solve the Group By statement error: this is incompatible with sql_mode = only_full_group_by

Today I wrote such a SQL statement in Mysql on my personal server: The execution even reported an error, the error information is as follows: My Mysql version is 5.7.22, which was newly installed when...

mysql5.7 query error this is incompatible with sql_mode = only_full_group_by

The server is running normally, and an error occurs when the machine is running Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘mgl.db_mglmro_customer_c...

MYSQL8.0 sort query error this is incompatible with sql_mode=only_full_group_by

Edit the mysql configuration file (windows is my.ini, linux is my.cnf), add the following configuration under [mysqld]:   Just restart the MySQL service...

Mysql: MySQL-this is incompatible with sql_mode=only_full_group_by error resolution

First, the background Previously used local server in development, local is mysql5.6, there is a problem when switching to online server Prompt MySQL-this is incompatible with sql_mode=only_full_group...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top