项目场景:
公司让在原有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
然后就解决了。