我有使用table1进行此查询
SELECT DATE_FORMAT(FROM_UNIXTIME(t.TSTAMP), "%Y-%m") AS "timePeriod", COUNT(*) AS "prod1"
使用col'timePeriod'和col'prod1'给出了完美的2列结果。我现在想更新table2.prod1,其中主键是'timePeriod'。
我试过
答案 0 :(得分:3)
这样的东西......我无法测试它。
UPDATE table2 AS T2
INNER JOIN
(
SELECT DATE_FORMAT(FROM_UNIXTIME(TSTAMP), "%Y-%m") AS "timePeriod",
COUNT(*) AS prod1 FROM table1
GROUP BY DATE_FORMAT(FROM_UNIXTIME(TSTAMP), "%Y-%m")
) AS T1 USING (timePeriod)
SET T2.prod1 = T1.prod1;
答案 1 :(得分:1)
这将按照您指定的方式执行
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)