Mac MySql8报错1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated

报错信息:

1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘fbjs.mscc.ContactTime’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by, Time: 0.000000s

报错原因:

在mysql5.7以上的版本中,对于 group by 的这种聚合操作,如果在select 中的列,没有在group by 中出现,那么这个SQL是不合法的,因为列不在group by的从句中,所以对于设置了这个mode的数据库,在使用group by 的时候,就要用MAX(),SUM(),ANT_VALUE()的这种聚合函数,才能完成GROUP BY 的聚合操作。

解决方法:

方法一:修改sql语句

把所有除了有聚合函数的字段都加在select后面的查询结果里。

方法二:修改数据库临时配置

执行sql语句:

set sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

方法三:修改数据库配置文件

  1. 修改/etc/my.cnf,将sql_mode=中的only_full_group_by给删掉
  2. 如果没有my.cnf文件则创建一个,mysql的配置文件路径查找优先级为/etc/my.cnf,/etc/mysql/my.cnf,/usr/local/etc/my.cnf,通过Homebrew安装的my.cnf放在/usr/local/etc/中。
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
 
[mysqld]
 
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
 
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
 
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
 
#服务端口号 默认3306
port = 3306
server_id = 1
# mysql安装根目录
basedir = /usr/local/mysql
# mysql数据文件所在位置
datadir = /usr/local/mysql/data
# pid
#pid-file = /usr/local/mysql/data/mysql.pid
# 设置socke文件所在目录
#socket = /usr/local/mysql/data/mysql.sock
 
# 跳过密码登录
# skip-grant-tables
 
# 数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
# character-set-server = utf8mb4
 
# 数据库字符集对应一些排序等规则,注意要和character-set-server对应
# collation-server = utf8mb4_general_ci
 
# 设置client连接mysql时的字符集,防止乱码
# init_connect='SET NAMES utf8mb4'
 
# 是否对sql语句大小写敏感,1表示不敏感,8.0需要在初始化时候设置
# lower_case_table_names = 1
 
# 最大连接数
max_connections = 1000
 
#最大错误连接数
max_connect_errors = 1200
 
 
# wait_timeout = 1814400
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 
 
 
# 二进制日志目录
#log_bin = /usr/local/mysql/logs/mysql-bin
# 自动删除过期日志的天数
expire_logs_days = 10
# 限制单个文件大小
max_binlog_size = 100M
 
# 查询日志
#general_log = 1
# 查询日志文件位置
#general_log_file = /usr/local/mysql/logs/query.log
 
# 数据库错误日志文件
#log_error = /usr/local/mysql/logs/error.log
 
 
# sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
  1. 给新创建的my.cnf授权644权限
chmod 644 /etc/my.cnf
  1. 修改mysql配置
    Mac MySql8报错1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated_第1张图片

  2. 重启mysql服务
    Mac MySql8报错1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated_第2张图片

你可能感兴趣的:(数据库,错误异常)