测试testAInsertProductSellDaily() 出现错误

测试testAInsertProductSellDaily() 出现错误

org.springframework.jdbc.BadSqlGrammarException: 

### Error updating database.  Cause: java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'o2o.tb_user_product_map.shop_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

### The error may involve com.fushan.o2o.dao.ProductSellDailyDao.insertProductSellDaily-Inline

### The error occurred while setting parameters

### SQL: INSERT     INTO     tb_product_sell_daily(product_id,shop_id,create_time,total)     (     SELECT     product_id,shop_id,date_format(create_time,'%Y-%m-%d'),count(product_id)     AS total FROM     tb_user_product_map     WHERE     date_format(create_time,'%Y-%m-%d') =     date_sub(curdate(),interval 1     day) GROUP BY product_id)

### Cause: java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'o2o.tb_user_product_map.shop_id' 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 'o2o.tb_user_product_map.shop_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by


http://img1.sycdn.imooc.com//climg/5dee3ce50954f64c07630106.jpg<insert id="insertProductSellDaily" parameterType="com.fushan.o2o.entity.ProductSellDaily">
   INSERT
   INTO
   tb_product_sell_daily(product_id,shop_id,create_time,total)
   (
   SELECT
   product_id,shop_id,date_format(create_time,'%Y-%m-%d'),count(product_id)
   AS total FROM
   tb_user_product_map
   WHERE
   date_format(create_time,'%Y-%m-%d') =
   date_sub(curdate(),interval 1
   day) GROUP BY product_id)
</insert>
   <insert id="insertDefaultProductSellDaily">
   INSERT
   INTO
   tb_product_sell_daily(product_id,shop_id,create_time,total)
   (
   SELECT
   p.product_id,p.shop_id,date_sub(curdate(),interval 1 day),0 AS
   total
   FROM
   tb_product p
   WHERE
   p.product_id not in (select product_id from
   tb_product_sell_daily where date_format(create_time,'%Y-%m-%d') =
   date_sub(curdate(),interval 1 day))
   )
</insert>

正在回答

登陆购买课程后可参与讨论,去登陆

5回答

同学你好。报错信息显示:

http://img1.sycdn.imooc.com//climg/5def10ca096db6b608420183.jpg

大致意思是:

选择列表的表达式“2”不在GROUP BY子句中,并且包含未聚合的列“o2o.tb_user_product_map.shop_id”,该列在功能上不依赖GROUP BY子句中的列;这与sql_mode=only_full_GROUP_BY不兼容

这和MySQL的版本有关,同学的版本是开启了严格模式的。

对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,也没有出现在聚合函数里面,那么这个SQL是不合法的,因为列不在GROUP BY从句中,也就是说查出来的列必须在group by后面出现。否则就会报错。

同学可以参考一下 第三阶段课程 MySQL基础之数据类型 http://class.imooc.com/course/888 的教辅 严格模式的关闭.pdf 将本地的数据库严格模式关闭后再试。

如果解答了同学的疑问,望采纳~

祝学习愉快~

提问者 学习中的傅山 2019-12-11 00:17:02
  • 提问者 学习中的傅山 #1
    没出现之前的错误了 但是不知道为什么没有生成3个数据
    2019-12-11 00:17:30
提问者 学习中的傅山 2019-12-11 00:11:41

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'quartzConfiguration': Unsatisfied dependency expressed through field 'productSellDailyService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productSellDailyServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productSellDailyDao' defined in file [E:\maven\bs\o2o\target\classes\com\fushan\o2o\dao\ProductSellDailyDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/fushan/o2o/config/dao/SessionFactoryConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [E:\maven\bs\o2o\target\classes\mapper\ShopAuthMapDao.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.imooc.o2o.entity.ShopAuthMap'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.imooc.o2o.entity.ShopAuthMap

老师重启完  出现了这个错误  

提问者 学习中的傅山 2019-12-10 12:21:52
  • 同学你好。请问报错信息还是原来的报错信息吗?祝学习愉快~
    2019-12-10 13:52:32
  • 提问者 学习中的傅山 回复 芝芝兰兰 #2
    是啊 老师
    2019-12-10 14:26:22
  • 芝芝兰兰 回复 提问者 学习中的傅山 #3
    同学打开cmd窗口,使用命令“net strat mysql”重新启动一下MySQL服务试试。如果不行,使用Win+R打开运行窗口,输入“services.msc”点击确定,打开服务窗口,找到MySQL服务,如果是5.7版本的有时候服务名叫5.7,然后点击启动,或者重启动。祝学习愉快~
    2019-12-10 17:25:32
提问者 学习中的傅山 2019-12-09 20:25:26
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师