我有一张表,我在上面做这个查询

SELECT DATE_FORMAT(FROM_UNIXTIME(t.TSTAMP), "%Y-%m") AS "timePeriod", COUNT(*) AS "prod1"

使用col'timePeriod'和col'prod1'给出完美的me 2列结果。我现在要更新table2.prod1,其中主键是“timePeriod”。
我试过了

最佳答案

这将按您指定的方式进行

INSERT
IGNORE // include this if you want conflicts to be ignored
INTO table2 (
    timePeriod,
    prod1
)
SELECT
    DATE_FORMAT(FROM_UNIXTIME(t.TSTAMP), "%Y-%m"),
    COUNT(*)

// you didn't include a "FROM" so added relevant place here
FROM table1

// Include this if you want conflicts to append to the existing row
ON DUPLICATE KEY UPDATE prod1 = prod1 + VALUES(prod1)

// Include this if you want conflicts to replace the existing row
ON DUPLICATE KEY UPDATE prod1 = VALUES(prod1)

关于mysql - 在一个表1中按月计数(Unix时间戳记),然后将结果写入表2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9331297/

10-09 17:50