我已经通过几个职位,但无法找到确切的解决办法。这是我想要的-
Table1 - column1, column2, column3, amount as column4
Table2 - column1, column2, column3, amount as column4
字符串
我使用下面的代码,它可以很好地插入,但对于更新,它不更新金额列
下面是代码
INSERT INTO table2 (column1,column2, column3, amount)
SELECT column1, column2, column3, sum(amount)
FROM table1
where column5 != '' and column5 != 'null' and column7 = 'good'
group by column1
ON DUPLICATE KEY UPDATE amount=SELECT sum(amount) FROM table1 where column5 != '' and column5 != 'null' and column7 = 'good' group by column1;
型
我知道错误是更新部分,因为当我使ON DUPLICATE KEY UPDATE amount=0
这工程和所有行更新为0
我在更新部分做错了什么。
谢谢你的帮助提前。
我想更新查询部分得到工作。
2条答案
按热度按时间raogr8fs1#
子查询在
ON DUPLICATE KEY UPDATE
上是不允许的,这里有一种方法可以做到这一点,而不需要重新计算总数,方法是使用一个派生表,该表允许引用GROUP BY
查询中的列:字符串
Demo here
velaa5lx2#
在子查询周围放置
()
。如下所示:字符串