1. 程式人生 > >Mysql解決SELECT list is not in GROUP BY clause and contains nonaggregated column 問題

Mysql解決SELECT list is not in GROUP BY clause and contains nonaggregated column 問題

轉自:https://blog.csdn.net/u011676300/article/details/79564446

在使用GROUP BY對Mysql的資料表進行查詢時如果出現以下錯誤

select * from user group by age;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause 
and contains nonaggregated column 'test.user.user_id' which is not 
functionally dependent on columns  in GROUP BY clause; this is 
incompatible with sql_mode=only_full_group_by

解決辦法: 
1.查詢mysql 相關mode

select @@global.sql_mode;

可以看到模式中包含了ONLY_FULL_GROUP_BY,只要沒有這個配置即可。 
我的Mysql版本是5.7.21,預設是帶了ONLY_FULL_GROUP_BY模式。

 ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,
 ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 

2.重設模式值

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

3.重啟Mysql即可