Cause: com.kingbase8.util.KSQLException: 错误: 字段 “xxx“ 必须出如今 ...

打印 上一主题 下一主题

主题 1039|帖子 1039|积分 3117

前言

在使用Kingbase8数据库时,我们大概会遇到一些与MySQL差异的SQL语法问题。本文将详细探究一个具体的错误案例,即在Kingbase8中使用GROUP BY子句时遇到的问题,并提供解决方案。
问题

又是熟悉的kingbase8 又是熟悉的报错 详细报错信息如下
  1. \### Error querying database. Cause: com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
  2. Position: 65 At Line: 4, Line Position: 26
  3. \### The error may exist in com/xx/xxx/mapper/OrderGoodsMapper.xml
  4. \### The error may involve com.xxx.xx.mapper.OrderGoodsMapper.selectSkuOrderCount-Inline
  5. \### The error occurred while setting parameters
  6. \### 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
  7. \### Cause: com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
  8. Position: 65 At Line: 4, Line Position: 26
  9. ; bad SQL grammar []; nested exception is com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
  10. Position: 65 At Line: 4, Line Position: 26
  11. 2024-04-25 17:56:29.822 [http-nio-8560-exec-5] ERROR [[d41cdf6dcdd22a35,,e8730f291be24b7b,] c.w.common.core.web.handler.GlobalExceptionHandler.handleException - Unknown exception type: org.springframework.jdbc.BadSqlGrammarException
  12. org.springframework.jdbc.BadSqlGrammarException:
  13. \### Error querying database. Cause: com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
  14. Position: 65 At Line: 4, Line Position: 26
  15. \### The error may exist in com/xxx/xx/mapper/OrderGoodsMapper.xml
  16. \### The error may involve com.xxx..mapper.OrderGoodsMapper.selectSkuOrderCount-Inline
  17. \### The error occurred while setting parameters
  18. \### 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
  19. \### Cause: com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
  20. Position: 65 At Line: 4, Line Position: 26
  21. ; bad SQL grammar []; nested exception is com.kingbase8.util.KSQLException: 错误: 字段 "se_order_goods.sku_url" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
  22. Position: 65 At Line: 4, Line Position: 26
复制代码
分析

  1. 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效果雷同。

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参数:
  1. TEST=# set sql_mode = 'ONLY_FULL_GROUP_BY';
  2. SET
  3. TEST=# show sql_mode;
  4.       sql_mode
  5. -----------------------------------
  6. ONLY_FULL_GROUP_BY
  7. (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企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

圆咕噜咕噜

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表