2013-12-09 262 views
2

我想運行一個INSERT查詢,基於SELECT,與「ON DUPLICATE KEY UPDATE」語句。 SELECT查詢工作正常,如果我要「手動」輸入結果數據,則會導致重複密鑰問題。到現在爲止還挺好。但是,下面的查詢似乎沒有像我預期的那樣更新「et_report_ymd.quotes」中的值。INSERT SELECT與ON DUPLICATE KEY UPDATE

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 

所有幫助是值得歡迎...

回答

2

而就當你決定尋求幫助,你拿出解決方案,一如既往。

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(引號)」部分,而不是「引號」。

相關問題