mybatis的group by有关的未解决问题

本文探讨了在mybatis中使用group by时遇到的问题,主要集中在连表查询与Mysql的区别。当attr_id和attr_name存在一一对应关系时,condition2能够正常运行,而condition1却导致错误。
摘要由CSDN通过智能技术生成

mybatis的group by有关的问题

连表查询中,attr_id和attr_name是一一对应的关系

#Condition1:GROUP BY pssav.`attr_id`
SELECT pssav.`attr_id`,pssav.`attr_name`,GROUP_CONCAT(DISTINCT pssav.`attr_value`) 
FROM `pms_sku_info` info 
LEFT JOIN `pms_sku_sale_attr_value` pssav ON info.`sku_id`=pssav.`sku_id`
WHERE info.`spu_id`=4
GROUP BY pssav.`attr_id`,pssav.`attr_name`

#Condition2:GROUP BY pssav.`attr_id`,pssav.`attr_name`;
SELECT pssav.`attr_id`,pssav.`attr_name`,GROUP_CONCAT(DISTINCT pssav.`attr_value`) 
FROM `pms_sku_info` info 
LEFT JOIN `pms_sku_sale_attr_value` pssav ON info.`sku_id`=pssav.`sku_id`
WHERE info.`spu_id`=4
GROUP BY pssav.`attr_id`,pssav.`attr_name`;

在Mysql中查询为一样的结果,但是在在mybatis中写sql语句时,condition2可以正常运行,但是condition1会报错

org.springframework.jdbc.BadSqlGrammarException: 
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'taomall_pms.pssav.attr_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
### The error may exist in file [D:\workspace\java\taomall\taomall-product\target\classes\mapper\product\SkuSaleAttrValueDao.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT             pssav.`attr_id` attr_id,             pssav.`attr_name` attr_name,             GROUP_CONCAT(DISTINCT pssav.`attr_value`) attr_values             FROM `pms_sku_info` info         LEFT JOIN `pms_sku_sale_attr_value` pssav ON info.`sku_id`=pssav.`sku_id`         WHERE info.`spu_id`=?         GROUP BY pssav.`attr_id`;
### Cause: java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'taomall_pms.pssav.attr_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'taomall_pms.pssav.attr_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

	at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:93)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractF
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值