MYSQL:有没有办法通过 ON DUPLICATE KEY UPDATE 从另一个 Table 插入值?

MYSQL: Is there a way to Insert Values from another Table with ON DUPLICATE KEY UPDATE?

我的问题: 有没有办法通过 ON DUPLICATE KEY UPDATE 从另一个 Table 插入值?但是我在这个语句中也使用了 SUM

这是我的例子:

INSERT INTO currencyStatistics (note, currency, amount) 
SELECT note, currency, SUM(amount) as amount2 
FROM currency 
WHERE date<1612188495 
GROUP BY note 
ON DUPLICATE KEY UPDATE amount=amount+amount2;

MySQL returns 错误:“#1054 - 'field list' 中的未知列 'amount2'”

奖金信息:Table currencyStatistics 有 4 列:note、currency、amount 和 lastUpdate。 我没有在插入时添加最后更新,导致它的时间戳默认为“current_timestamp(6)”和额外的“ON UPDATE CURRENT_TIMESTAMP(6)”

有人可以帮我解决这个问题吗?

你好

编辑:添加了正确的错误消息

INSERT INTO currencyStatistics (note, currency, amount) 
SELECT note, currency, SUM(amount)
FROM currency 
WHERE date<1612188495 
GROUP BY note 
ON DUPLICATE KEY 
UPDATE amount = amount + VALUES(amount);

更新中:

  • amount 是更新后的列 table (currencyStatistics)
  • 如果没有重复(即 SUM(amount) 值),
  • VALUES(amount) 是本应插入到 amount 中的值