MySQL报错: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column

这个错误主要是 select 的内容里的字段没有再group by 中出现

这里提供两种解决办法:

第一种:

在你的MySQL的my.ini文件里面加一条

[mysqld]
sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
 

第二种:

我本来用的是

SELECT
 *
FROM
 biz_table bt
 LEFT JOIN biz_table_ver btv ON bt.table_id = btv.table_id 
WHERE
 bt.system_up = 1 
GROUP BY
 bt.table_name

这种写法报错

 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'zx_ds.biz_table_ver.ver_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by\n### The error may exist in URL [jar:file:/opt/zxs/ds/ds-dict.jar!/BOOT-INF/classes!/mapper/system/BizTableMapper.xml]\n### The error may involve com.gyjn.dict.data.mapper.BizTableMapper.selectBizTableList-Inline\n### The error occurred while setting parameters\n### SQL: select         *        FROM         biz_table  bt         LEFT JOIN ( SELECT * FROM biz_table_ver GROUP BY table_id ) btv ON bt.table_id = btv.table_id          WHERE  bt.system_up=1\n### Cause: java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'zx_ds.biz_table_ver.ver_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by\n; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'zx_ds.biz_table_ver.ver_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

后来改为

SELECT
	* 
FROM
	biz_table_ver v
	INNER JOIN biz_table t
	INNER JOIN ( SELECT max( ver_id ) ver_id, table_id FROM biz_table_ver GROUP BY table_id ) maxv ON v.ver_id = maxv.ver_id 
	AND t.table_id = maxv.table_id

就可以了

猜你喜欢

转载自blog.csdn.net/m0_57666466/article/details/125395289
今日推荐