MySQL uses group_concat in aggregated query without group by

tags: mysql  

Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'ie_llyh.u.ChkPlanID'; this is incompatible with sql_mode=only_full_group_by
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'ie_llyh.u.ChkPlanID'; this is incompatible with sql_mode=only_full_group_by

1 Reason:

Use group_concat in SQL to combine, if you are other group by reason, please find other solutions

Online explanation:

After mysql5.7.5, the default opened online, so some SQL could not be executed normally, in fact, it is caused by our SQL irregular, because some of the data returned is uncertain, so this will appear mistake.

2 solution

method one

Code modified SQL

Rewriting involving the SQL section of Group_Concat

example

Table Class Class Table

  • Field CID class ID

ClassUser class student correspondence table

  • Field ID primary key
  • Field CID class ID
  • Field UID Student ID

Table UserInfo student information table

  • Field UID Student ID
  • Field Name Student Name

Original code

 select a.cid
        GROUP_CONCAT(c.name SEPARATOR ',') as allName
             from class a
        LEFT JOIN classUser b ON a.cid = b.cid
        LEFT JOIN UserInfo c ON c.uid = b.uid
        WHERE 1=1 

After modification

   select   a.cid
        b.name as allName
        from class a
         LEFT JOIN (
			select  GROUP_CONCAT(b.name SEPARATOR ',') as name , a.cid  from classUser  a 
            left join UserInfo b on a.uid = b.uid
			group by a.cid 
        )b on a.cid = b.cid
        where 1=1

Method 2 (online method is not verified)

Close ONLY_FULL_GROUP_BY, I am Linux environment, I just talk about Linux solution:

Log in into MySQL

Linux login: mysql -u username -p, then enter the password, enter SQL: show variables like '% SQL_MODE';

Edit my.cnf file

File address is usually: /etc/my.cnf/eetc/mysql/my.cnf
Find the position of SQL-mode, remove online, then restart mysql
There is no SQL-MODE in my.cnf, you need to join:

sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 

Restart the mysql service after successful

Service MySQL Restart, after restart, log in to Mysql, enter SQL: show variables like '% SQL_MODE'; if there is no online, it is already successful.

Intelligent Recommendation

Use mysql DISTINCT function, given ERROR 1140 (42000): In aggregated query without GROUP BY .....

phenomenon: Performing sql SELECT COUNT (DISTINCT (colum1)), colum2, colum3 FROM table; error ERROR 1140 (42000): In aggregated query without GROUP BY ..... the reason: sql_mode default mysql opens up...

mysql error 1140 In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregate

In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column'szmz_zhbz_gov.b.cremation_time'; this is incompatible with sql_mode=only_full_group_by After reviewing ...

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

Preface Operating todaymysqlWhen querying data in the database, a prompt is encountered, the query fails, and the prompt message is as follows: When running this SQL statement, there is no problem: Bu...

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...

MySQL error: in aggregated query without group by, expression # 2 of success list contacts nona Select List Contains non

reason: 1. The official explanation of this mode: Only_Full_Group_by is a SQL_Mode provided by the MySQL database, guaranteed by this SQL_Mode, SQL statement "the highest value" legality che...

More Recommendation

MySQL || Removement: in aggregated query without group by, expression # 1 of success list contains nonaGregate

Shatter Brother Meng, I Hu Han San is coming back again (.. ∀ ·) ノ゙ ノ゙¯ ▽ ¯ *) § First, the problem description Today, I do SQL in the cow passenger net: the course of the...

Error 1140: In aggregated query without GROUP BY,sql_mode=only_full_group_by

There is no problem running on MySQL 5.5.27. The error of sql_mode=only_full_group_by is reported on MySQL 5.7.24: Error 1140: In aggregated query without GROUP BY, expression #1 of SELECT list contai...

In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated

Record an error in a mysql combination query plus grouping: In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'tsbio.sl.id'; this is incompatible with sq...

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

Article Directory Problem description Problem traceability Solution 1 Solution 2 appendix: Problem description Here is a screenshot of the error: Translated: In an aggregation query without group BY, ...

mysql group query GROUP_CONCAT filter data

A problem usually encountered when writing sql, record it There are three tables student (student table), teacher (teacher table), stu_teacher (student-teacher relationship table) The table structure ...

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

Top