group by 聚合操作出错解决办法
例如:查找Sell表中会员订单数在2笔以上且每笔订购册数都在5本以上的会员。
SELECT 用户号, 订购册数
FROM Sell
WHERE 订购册数 >5
GROUP BY 用户号
HAVING COUNT(*) > 2;
执行select语句报错:Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ui.Sell.订购册数' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
原因分析:group by 聚合操作的时候,进行查询操作的列没有在group by 中出现,那么这个sql就是不合法的。主要原因就是设置的与sql_mode=only_full_group_by不兼容。mysql5.7.5及以上的版本都会依赖检测功能,也就是默认会开启only_full_group_by。
问题解决办法:
(1)执行语句
set @@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
(2)重启mysql,问题解决。