Cause: com.kingbase8.util.KSQLException: 错误: 字段 “xxx“ 必须出如今
前言在使用Kingbase8数据库时,我们大概会遇到一些与MySQL差异的SQL语法问题。本文将详细探究一个具体的错误案例,即在Kingbase8中使用GROUP BY子句时遇到的问题,并提供解决方案。
问题
又是熟悉的kingbase8 又是熟悉的报错 详细报错信息如下
\### Error querying database. Cause: com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
Position: 65 At Line: 4, Line Position: 26
\### The error may exist in com/xx/xxx/mapper/OrderGoodsMapper.xml
\### The error may involve com.xxx.xx.mapper.OrderGoodsMapper.selectSkuOrderCount-Inline
\### The error occurred while setting parameters
\### SQL: SELECT count( * ) FROM (SELECT sku_code,sku_url,spu_name,sku_spec,sku_cost_price,sum(goods_quantity) as saleQuantity,sum(total_pay_price) as sale ,channel_mall_id FROM se_order_goods WHERE pay_status != 0 and channel_customer_id = ? and goods_type = ? and pay_time >= ? and pay_time <= ? GROUP BY sku_code ) a
\### Cause: com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
Position: 65 At Line: 4, Line Position: 26
; bad SQL grammar []; nested exception is com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
Position: 65 At Line: 4, Line Position: 26
2024-04-25 17:56:29.822 ERROR [ c.w.common.core.web.handler.GlobalExceptionHandler.handleException - Unknown exception type: org.springframework.jdbc.BadSqlGrammarException
org.springframework.jdbc.BadSqlGrammarException:
\### Error querying database. Cause: com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
Position: 65 At Line: 4, Line Position: 26
\### The error may exist in com/xxx/xx/mapper/OrderGoodsMapper.xml
\### The error may involve com.xxx..mapper.OrderGoodsMapper.selectSkuOrderCount-Inline
\### The error occurred while setting parameters
\### SQL: SELECT count( * ) FROM (SELECT sku_code,sku_url,spu_name,sku_spec,sku_cost_price,sum(goods_quantity) as saleQuantity,sum(total_pay_price) as sale ,channel_mall_id FROM se_order_goods WHERE pay_status != 0 and channel_customer_id = ? and goods_type = ? and pay_time >= ? and pay_time <= ? GROUP BY sku_code ) a
\### Cause: com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
Position: 65 At Line: 4, Line Position: 26
; bad SQL grammar []; nested exception is com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
Position: 65 At Line: 4, Line Position: 26
分析
SELECT count( * ) FROM (SELECT sku_code,sku_url,spu_name,sku_spec,sku_cost_price,sum(goods_quantity) as saleQuantity,sum(total_pay_price) as sale ,channel_mall_id FROM se_order_goods WHERE pay_status != 0 and channel_customer_id = ? and goods_type = ? and pay_time >= ? and pay_time <= ? GROUP BY sku_code ) a
上述SQL 也不是很复杂 在mysql中 正常运行 但是在 在kingbase中 居然报错了
kingbase8 的数据库修改 group by的语法 并不能和MySQL 的group by语法 兼容
官方文档:
group by 操作 ¶
问题描述:
KES数据库group by 操作与mysql 效果差异
分析与解决方法:
MySQL 的group by : MySQL支持selectlist中非聚集列可以不出如今group by中。sql尺度是必须出如今group by中, 如:select col1, col2 from tab group by col1; kingbase 为兼容mysql,设置了个参数ql_mode参数,目前只支持ONLY_FULL_GROUP_BY选项。如果sql_mode中不包含ONLY_FULL_GROUP_BY,group by语句可以不符合sql尺度。 也就是与mysql效果雷同。
https://i-blog.csdnimg.cn/direct/1428008f607b4c3b9f6c742feee68e0f.png
sql_mode :兼容MySQL的模式列表。
在KingbaseES系统参数 sql_mode 可以设置为:
ONLY_FULL_GROUP_BY :如果查询的select列表、HAVING条件或ORDER BY列表,引用了既不在GROUP BY子句中定名,也不再功能上依靠于GROUP_BY列的非聚合列,则拒绝这些查询;
STRICT_ALL_TABLES :为所有存储引擎启用严格SQL模式,无效的数据值将被拒绝;
REAL_AS_FLOAT :REAL范例默认是FLOAT8,开启之后为FLOAT4;
NO_AUTO_VALUE_ON_ZERO :开启时,插入0序列不自增,将0作为终极插入的值;
ANSI_QUOTES :表示双引号返回标示符。如果用户取消该选项,则表示双引号返回字符常量。注意:若取消该模式,将会导致Studio工具新建、删除对象失败。
可以用如下方式使用sql_mode参数:
TEST=# set sql_mode = 'ONLY_FULL_GROUP_BY';
SET
TEST=# show sql_mode;
sql_mode
-----------------------------------
ONLY_FULL_GROUP_BY
(1 行记录)
解决
这文档写的不是很明确
会话收效 set sql_mode = ‘’; show sql_mode;
全局收效 kingbase.conf中配置sql_mode= ‘’ 然后重启数据库服务,可以兼容这种sql模式
附上官方的文档: https://help.kingbase.com.cn/v8/development/develop-transfer/transplant-mysql/transplant-mysql-3.html#id9
good day!!!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]