I want to GROUP and aggregate the rows from one table and insert into the other, and update the aggregate value. What is the SQL command for that?
INSERT INTO tbdetail (detail, views)
(SELECT detail, SUM(views) AS viewsall
FROM tb
GROUP BY detail)
//ON DUPLICATE KEY UPDATE views = views + viewsall <- how to update here?
In MySQL, when using an
INSERT ... SELECTstatement, you can refer to one of the values of theSELECTstatement using theVALUES()function:More information here: http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_values