MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案

项目场景:

公司让在原有sql上增加一个公司名称字段。
但是当执行group by时,select的字段不属于group by的字段的话,sql语句就会报错。报错信息如下:

Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column ‘数据库名.表名.字段名’ which is not functionally dependent
on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by

问题描述:

1原始sql

SELECT
	(
	SELECT
		t2.exam_name 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS exam_name,
	(
	SELECT
		t2.emp_code 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS emp_code,
	(
	SELECT
		t2.emp_name 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS emp_name,
	(
	SELECT
		t2.org_name 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS org_name,
	(
	SELECT
		t2.reply_total_time 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS reply_total_time,
	(
	SELECT
		t2.reply_total_grade 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS reply_total_grade,
	(
	SELECT
		t2.test_my_nums 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS test_my_nums,
	(
	SELECT
		t2.`status` 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS examStatus,
	t3.pass_line AS passLine
FROM
	t_ks_myreplyinfo t1
	LEFT JOIN t_ks_exam t3 ON t1.exam_id = t3.exam_id 
WHERE
	t1.delete_flag =1
GROUP BY
	t1.exam_id,
	t1.emp_code 
ORDER BY
	t3.exam_name,
	t1.emp_code 
	LIMIT 10

加入子查询字段sql

(
		SELECT
			SUBSTRING_INDEX( SUBSTRING_INDEX( t_xt_org_base_info.org_full_name, "|", 2 ), "|",- 1 ) 
		FROM
			t_xt_org_base_info 
		WHERE
			t_xt_org_base_info.org_code = t1.org_code 
		) companyName 

加入后变为

SELECT
	(
	SELECT
		t2.exam_name 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS exam_name,
	(
	SELECT
		t2.emp_code 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS emp_code,
	(
	SELECT
		t2.emp_name 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS emp_name,
	(
	SELECT
		t2.org_name 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS org_name,
	(
	SELECT
		t2.reply_total_time 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS reply_total_time,
	(
	SELECT
		t2.reply_total_grade 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS reply_total_grade,
	(
	SELECT
		t2.test_my_nums 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS test_my_nums,
	(
	SELECT
		t2.`status` 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS examStatus,
	t3.pass_line AS passLine,
(
		SELECT
			SUBSTRING_INDEX( SUBSTRING_INDEX( t_xt_org_base_info.org_full_name, "|", 2 ), "|",- 1 ) 
		FROM
			t_xt_org_base_info 
		WHERE
			t_xt_org_base_info.org_code = t1.org_code 
		) companyName 
FROM
	t_ks_myreplyinfo t1
	LEFT JOIN t_ks_exam t3 ON t1.exam_id = t3.exam_id 
WHERE
	t1.delete_flag =1
GROUP BY
	t1.exam_id,
	t1.emp_code 
ORDER BY
	t3.exam_name,
	t1.emp_code 
	LIMIT 10

执行报错:

> 1055 - Expression #10 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'qnaq.t1.org_code' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

翻译:

“错误代码:1055。SELECT列表的表达式#1不在GROUP BY子句中,并且包含非聚合列’test.t_iov_help_feedback.ID’,它在功能上不依赖于GROUP BY子句中的列; 这与sql_mode = only_full_group_by不兼容” 

原因分析:

一、原理层面
这个错误发生在mysql 5.7.5 版本及以上版本会出现的问题:
mysql 5.7.5版本以上默认的sql配置是:sql_mode=“ONLY_FULL_GROUP_BY”,这个配置严格执行了"SQL92标准"。
很多从5.6升级到5.7时,为了语法兼容,大部分都会选择调整sql_mode,使其保持跟5.6一致,为了尽量兼容程序。

二、sql层面
在sql执行时,出现该原因,简单来说就是:
由于开启了ONLY_FULL_GROUP_BY的设置,如果select 的字段不在 group by 中,
并且select 的字段未使用聚合函数(SUM,AVG,MAX,MIN等)的话,那么这条sql查询是被mysql认为非法的,会报错误

解决方案:

解决方案一:使用函数ANY_VALUE()包含报错字段

将上述报错语句改成:

SELECT
	(
	SELECT
		t2.exam_name 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS exam_name,
	(
	SELECT
		t2.emp_code 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS emp_code,
	(
	SELECT
		t2.emp_name 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS emp_name,
	(
	SELECT
		t2.org_name 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS org_name,
	(
	SELECT
		t2.reply_total_time 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS reply_total_time,
	(
	SELECT
		t2.reply_total_grade 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS reply_total_grade,
	(
	SELECT
		t2.test_my_nums 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS test_my_nums,
	(
	SELECT
		t2.`status` 
	FROM
		t_ks_myreplyinfo t2 
	WHERE
		t2.exam_id = t1.exam_id 
		AND t2.emp_code = t1.emp_code 
	ORDER BY
		t2.reply_total_grade DESC 
		LIMIT 1 
	) AS examStatus,
	t3.pass_line AS passLine,
	ANY_VALUE ((
		SELECT
			SUBSTRING_INDEX( SUBSTRING_INDEX( t_xt_org_base_info.org_full_name, "|", 2 ), "|",- 1 ) 
		FROM
			t_xt_org_base_info 
		WHERE
			t_xt_org_base_info.org_code = t1.org_code 
		)) companyName 
FROM
	t_ks_myreplyinfo t1
	LEFT JOIN t_ks_exam t3 ON t1.exam_id = t3.exam_id 
WHERE
	t1.delete_flag =1
GROUP BY
	t1.exam_id,
	t1.emp_code 
ORDER BY
	t3.exam_name,
	t1.emp_code 
	LIMIT 10

 可以看到,结果能正常查询了,根据需要自己改查询字段的别名就行.

ANY_VALUE()函数说明:

MySQL有any_value(field)函数,它主要的作用就是抑制ONLY_FULL_GROUP_BY值被拒绝。
这样sql语句不管是在ONLY_FULL_GROUP_BY模式关闭状态还是在开启模式都可以正常执行,不被mysql拒绝。
any_value()会选择被分到同一组的数据里第一条数据的指定列值作为返回数据。
官方有介绍,地址:https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value

然后就解决了。 

MySQL中出现 `sql_mode=only_full_group_by` 错,主要是由于 MySQLSQL 模式(sql_mode)中启用了 `ONLY_FULL_GROUP_BY` 规则。该规则要求 `SELECT` 查询中的所有非聚合列都必须出现在 `GROUP BY` 子句中,否则会触发错误。以下是几种解决方法: ### 1. 临时修改 SQL 模式 可以在当前会话或全局范围内临时禁用 `ONLY_FULL_GROUP_BY` 模式,以快速解决问题。例如: ```sql -- 临时移除 ONLY_FULL_GROUP_BY 模式 SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); ``` 此方法适用于需要快速修复问题的场景,但重启 MySQL 后设置会失效。 ### 2. 永久修改 SQL 模式 通过修改 MySQL 的配置文件(如 `my.cnf` 或 `my.ini`),永久移除 `ONLY_FULL_GROUP_BY` 模式。在配置文件中添加以下内容: ```ini [mysqld] sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION ``` 保存文件后重启 MySQL 服务,以应用新的 SQL 模式设置。 ### 3. 修改查询语句以符合 `ONLY_FULL_GROUP_BY` 要求 如果希望保留 `ONLY_FULL_GROUP_BY` 模式,可以通过调整 SQL 查询语句来避免错。确保 `SELECT` 子句中的所有非聚合列都出现在 `GROUP BY` 子句中。例如: ```sql -- 错误示例 SELECT department, name FROM employees GROUP BY department; -- 正确示例 SELECT department, MAX(name) FROM employees GROUP BY department; ``` 在此示例中,`MAX(name)` 是一个聚合函数,用于避免因 `name` 未出现在 `GROUP BY` 子句中而错。 ### 4. 使用 `ANY_VALUE()` 函数 如果某些列不需要聚合,但希望避免错,可以使用 `ANY_VALUE()` 函数。例如: ```sql SELECT department, ANY_VALUE(name) FROM employees GROUP BY department; ``` 此方法允许在 `SELECT` 子句中包含未出现在 `GROUP BY` 子句中的非聚合列。 ##
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值