报错是说语法错了 难道是因为我的数据库的问题
<insert id="insertProductSellDaily" parameterType="ProductSellDaily"> insert into tb_product_sell_daily (product_id,shop_id,create_time,total) values ( select product_id,shop_id,create_time,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 ) <!--调用group by count才能生效--> </insert> </mapper>
SQL: insert into tb_product_sell_daily (product_id,shop_id,create_time,total) values ( select product_id,shop_id,create_time,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: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select product_id,shop_id,create_time,count(product_id) as total from
tb' at line 3
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select product_id,shop_id,create_time,count(product_id) as total from
tb' at line 3
正在回答 回答被采纳积分+1
同学你好!
第一个报错是因为多写了values
同学所说的老师的代码的问题,是同学忘记写要插入的对应的列了呢
老师的代码是下面样子:
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 id="insertProductSellDaily" parameterType="ProductSellDaily"> insert into tb_product_sell_daily ( select product_id,shop_id,create_time,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 ) <!--调用group by count才能生效--> </insert>
改成和老师一样也报错
### The error occurred while setting parameters
### SQL: insert into tb_product_sell_daily ( select product_id,shop_id,create_time,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.SQLException: Column count doesn't match value count at row 1
; bad SQL grammar []; nested exception is java.sql.SQLException: Column count doesn't match value count at row 1
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星