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.

  1. create temporary table
  2. select aggregated data original
  3. then delete data in original table
  4. 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

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -