mysql - How to sum the duplicate values? -
i have mysql table rows containing duplicate values of text
('a'
, 'c'
):
+------+-----+ | text | num | +------+-----+ | | 10 | | b | 10 | | c | 10 | | d | 10 | | c | 5 | | z | 10 | | | 6 | +------+-----+
so, want update these rows summing values of num
. after table should this:
+------+-----+ | text | num | +------+-----+ | | 16 | | b | 10 | | c | 15 | | d | 10 | | z | 10 | +------+-----+
upd: edited question.
you can create temporary table store aggregated data temporarily , update original table it.
- create temporary table
- select aggregated data original
- then delete data in original table
- and select aggregated data temporary table original table.
example sql:
begin; create temporary table `table_name_tmp` `table_name`; insert `table_name_tmp` select `text`, sum(num) num `table_name` group 1; delete `table_name`; insert `table_name` select * `table_name_tmp`; -- commit;
i commented out commit
command avoid unwanted errors, please check results before using it.
Comments
Post a Comment