使用ON DUPLPLATE KEY UPDATE从SELECT插入

时间:2013-12-09 22:30:32

标签: mysql select insert on-duplicate-key

我正在尝试基于SELECT运行INSERT查询,并使用“ON DUPLICATE KEY UPDATE”语句。 SELECT查询有效,如果我“手动”输入结果数据将导致重复的密钥问题。到现在为止还挺好。但是,下面的查询似乎没有像我预期的那样更新“et_report_ymd.quotes”中的值。

INSERT IGNORE INTO et_report_ymd
SELECT 
    NULL,
    t.year AS year,
    t.month AS month,
    t.day AS day,
    SUM(t.quotes) AS quotes

FROM source_table AS t

GROUP BY t.year, t.month, t.day

ON DUPLICATE KEY UPDATE 
    et_report_ymd.quotes = quotes

欢迎所有帮助......

1 个答案:

答案 0 :(得分:3)

当你决定寻求帮助时,你会一如既往地提出解决方案。

INSERT IGNORE INTO et_report_ymd
SELECT 
    NULL,
    t.year AS year,
    t.month AS month,
    t.day AS day,
    SUM(t.quotes) AS quotes

FROM source_table AS t

GROUP BY t.year, t.month, t.day

ON DUPLICATE KEY UPDATE 
et_report_ymd.quotes = VALUES(quotes)

请注意查询末尾的“VALUES(quotes)”部分,而不仅仅是“引号”。