mysql:In aggregated query without GROUP BY, expression #1 of SELECT list contains...........

tags: 【mysql topic】  mysql  database  sql

Problem description

Here is a screenshot of the error:

1140 - In aggregated query without GROUP BY, expression
 #1 of SELECT list contains nonaggregated column 'supermarket.Order.Province';
  this is incompatible with sql_mode=only_full_group_by

Translated: In an aggregation query without group BY, expression #1 of the SELECT list contains a non-aggregated column’
The reason is: when the sql_mode of mysql is only_full_group_by, if the group by is not used and the aggregate function appears after the select, then all selected should be the aggregate function, otherwise an error will be reported.

Problem traceability

There is a STRICT mode in mysql5.7 or above, and in this mode, the default setting of the date value is not allowed to be all 0 values. Therefore, to solve this problem, you need to modify the value of sql_mode.

Solution 1

This solution is a quick solution of two commands. MySQL will be invalid after restarting (can be understood as temporary)

select version(), @@sql_mode;

SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

Check it out again:select version(), @@sql_mode;

Then enter our command to test
Here is the corresponding SQL statement

SELECT
  `province`,
	`City`,
	SUM(`Sales`) as  Total sales
FROM
	 	Order   
ORDER BY
	 Province


The mission is done!

Solution 2

Find the mysql configuration file
my.ini

The ones under Linux are also very different, just find the corresponding configuration directory ~

After saving the file, use the command line to restart the MySQL database service:

Stop/start: net stop mysql/net start mysql


After execution, execute in the database

set @@global.sql_mode = `STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION`;

This is executed in navicat, the effect is the same ~

In useselect @@global.sql_modeONLY_FULL_GROUP_BY does not exist in the query result

Write to our SQL language and execute perfectly~

appendix:

An introduction to several common SQL_mode values ​​is attached:

 Several commonmodeintroduce 

ONLY_FULL_GROUP_BY:
 Appear inselectStatement,HAVINGConditions andORDER BYThe column in the statement must beGROUP BYor depend onGROUP BYcolumn function column. 

NO_AUTO_VALUE_ON_ZERO:
 This value affects the insertion of the self-growing column. Under default settings, insert0orNULLRepresents the generation of the next self-growth value. If the user
 The value you want to insert is0, and the column is self-growing, then this option is useful. 

STRICT_TRANS_TABLES:
 In this mode, if a value cannot be inserted into a transaction table, the current operation will be interrupted and no restrictions will be imposed on non-transaction tables

NO_ZERO_IN_DATE:
 This mode affects whether the month and day in the date are allowed to be included0. If this mode is enabled,2016-01-00It is not allowed, but0000-02-01It is allowed. Its actual behavior is subject to strict modeImpact of whether it is turned on1。

NO_ZERO_DATE:
 Set this value, the mysql database does not allow insertion of zero dates. Its actual behavior is subject to strict
modeImpact of whether it is turned on2。

ERROR_FOR_DIVISION_BY_ZERO:
 existINSERTorUPDATEDuring the process, if the data is divided by zero, an error rather than a warning is generated. like 
 If this pattern is not given, MySQL returns when the data is divided by zero.NULL

NO_AUTO_CREATE_USER:
 prohibitGRANTCreate a user with empty password

NO_ENGINE_SUBSTITUTION:
 If the required storage engine is disabled or not compiled, an error is thrown. If this value is not set, use the default storage engine instead and throw an exception

PIPES_AS_CONCAT:
 Will"||"Be regarded as a string concatenation operator rather than a or operator, which is the same as Oracle database and is also similar to the string concatenation function Concat

ANSI_QUOTES:
 After ANSI_QUOTES is enabled, strings cannot be quoted in double quotes because they are interpreted as identifiers

Intelligent Recommendation

MySQL:ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list c

This wrong Chinese translation: In the aggregate query without using the Group By clause, the first expression in the SELECT list contains non-polymeric column 'school.student.s_id'; this is not possi...

Mysql error 1140 - in aggregated query without group by, express number # 1 of success list contains nonaGgreg

Execute SQL query statement: Wrong [Err] 1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'cust_cx.c.card_type_name'; this is incompatible with s...

The expression of the SELECT list is not in the GROUP BY clause and contains non-aggregated columns, which is incompatible with sql_mode = only_full_group_by

In the exercise, the most expensive product in the query group is reported as an error Error 1055 (42000): The expression #2 of the select list is not in the GROUP BY clause, and it contains mugua.goo...

Mysql join table query group error report Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre problem

It’s the first time I’m writing a blog, and I’ll record it. I’m a vegetable dog. For linked list queries, I’m reporting a grouping error. Baidu has been working on it for...

Expression # 1 of SELECT list is not in GROUP BY clause and contains nonaggre error when MySQL query

Expression # 1 of SELECT list is not in GROUP BY clause and contains nonaggre… a lot behind That's what went wrong We can see many explanations online MySQL 5.7.5 and above functions depend on ...

More Recommendation

mysql query report [Err] 1055-Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregat

1. The error message is as follows: 2. Reasons: MySQL 5.7.29 and up implement the detection of functional dependency. If only_full_group_by SQL mode is enabled (it is by default), MySQL will reject qu...

Mysql encountered Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

The error is as follows: Expression #2 of SELECT list is not in GROUP BY clause and contains  nonaggregated column ‘sss.month_id’ which is not functionally  dependent on columns ...

Mysql error Expression #1 of SELECT list is not in GROUP BY clause and contains non

The project is running well. After changing the database server, I reported this error. version reason Recording solution If the permissions are not enough Give users permission Or use the tool to cha...

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

table of Contents Solution Why is the ONLY_FULL_GROUP_BY limit set by default? Advanced discussion   Original link: https://blog.csdn.net/hq091117/article/details/79065199 https://blog.csdn.net/a...

Mysql error "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre"

- Throw an exception - solution Execute SQL against sql_mode=only_full_group_by, remove ONLY_FULL_GROUP_BY...

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

Top