我想显示总购买量,然后是购买表中的日期,以及按日期插入到total_purchase表中以及购买和查看
my purchase table structure as below:
id|companyName |purchase|date
1|housedotcom |1300 |2016-1-1
2|homedotcom |1234 |2016-1-1
3|gardendotco |1000 |2016-1-2
4|landotcom |999 |2016-1-2
5|garagedotcom|5400 |2016-1-2
6|homedotcom |2600 |2016-1-2
7|housedotcom |1900 |2016-1-2
my total_purchase table as below
id|date |buy
1|2016-1-1|2534
2|2016-2-2|20890
我试过这个到sql
INSERT INTO 'total_purchase'(date,buy) SELECT date, sum(purchase)
*FROM 'purchase' GROUP BY date;
它在mysql结果中显示我的预期,但当我尝试将新数据插入购买表中作为相同的companyName与mysql中的不同日期时,它表示重复以及在php编码sql中它没有工作并显示此错误
处理RequestSQLSTATE时出错[42000]:语法错误或访问冲突:1064 SQL语法错误;检查与您的MySQL服务器版本对应的手册,以便在“FROM purchaseGROUP BY date”附近使用正确的语法
有什么建议吗?提前谢谢。
答案 0 :(得分:1)
从查询中删除*为:
INSERT INTO total_purchase (date,buy) SELECT date, sum(purchase)
FROM purchase GROUP BY date;
答案 1 :(得分:0)
对于插入值,应该逐列输入。在选择中使用*返回行多个删除' *'
INSERT INTO 'total_purchase'(date,buy) SELECT date, sum(purchase)
FROM 'purchase' GROUP BY date;