All pastes #488722 Raw Edit

Something

public text v1 · immutable
·published 2007-05-15 04:08 UTC
rendered paste body
I have the following:

table1:
+-----+-----+-------+
| nid | vid | count |
+-----+-----+-------+
|  1  |  1  |  15   |
|  1  |  2  |  5    |
|  2  |  3  |  7    |
+-----+-----+-------+

I'm looking for an SQL query that can do an INSERT ... SELECT statement to give me this:

table2:
+-----+-------------+
| nid | total_count |
+-----+-------------+
|  1  |     20      |
|  2  |      7      |
+-----+-------------+

Possible using just SQL?

INSERT INTO table2 (nid, total_count) SELECT DISTINCT (nid), SUM(total_count) FROM table1 WHERE table2.nid = table1.nid

doesn't work....